Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 355010b

Browse files
Merge pull request fnplus#580 from iamvkundra/master
Created Program of ShellSort
2 parents 47d7e25 + b3fc3bd commit 355010b

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
class ShellSort
3+
{
4+
5+
static void printArray(int arr[])
6+
{
7+
int n = arr.length;
8+
for (int i=0; i<n; ++i)
9+
System.out.print(arr[i] + " ");
10+
System.out.println();
11+
}
12+
13+
/* function to sort arr using shellSort */
14+
int sort(int arr[])
15+
{
16+
int n = arr.length;
17+
18+
// Start with a big gap, then reduce the gap
19+
for (int gap = n/2; gap > 0; gap /= 2)
20+
{
21+
22+
for (int i = gap; i < n; i += 1)
23+
{
24+
// add a[i] to the elements that have been gap
25+
// sorted save a[i] in temp and make a hole at
26+
// position i
27+
int temp = arr[i];
28+
29+
// shift earlier gap-sorted elements up until
30+
// the correct location for a[i] is found
31+
int j;
32+
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
33+
arr[j] = arr[j - gap];
34+
35+
// put temp (the original a[i]) in its correct
36+
// location
37+
arr[j] = temp;
38+
}
39+
}
40+
return 0;
41+
}
42+
43+
// Driver method
44+
public static void main(String args[])
45+
{
46+
int arr[] = {12, 34, 54, 2, 3};
47+
System.out.println("Array before sorting");
48+
printArray(arr);
49+
50+
ShellSort ob = new ShellSort();
51+
ob.sort(arr);
52+
53+
System.out.println("Array after sorting");
54+
printArray(arr);
55+
}
56+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /