-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShellSort.java
67 lines (54 loc) · 2.27 KB
/
ShellSort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
public class ShellSort {
static void Sort(int []array, int number_of_elements)
{
int increment, temp,j ;
for(increment = number_of_elements/2 ; increment > 0 ; increment /= 2)
{
for(int i = increment; i<number_of_elements; i++)
{
temp = array[i];
for(j = i; j >= increment ; j-=increment)
{
for(int k=0 ; k<array.length ; k++)
{
System.out.print(array[k]+" ");
}
// understanding indexes
System.out.println("Increment with-"+increment+" "+array[j-increment]+" vs "+temp+" (j-increment) "+ (j-increment)+ "th Compare with "+j+"th" );
if(temp < array[j-increment])
{
array[j] = array[j-increment];
}
else
{
System.out.print("break\n");
break;
}
}
array[j] = temp;
for(int k=0 ; k<array.length ; k++)
{
System.out.print(array[k]+" ");
}
System.out.print("\n");
}
}
// print result
for(int i=0 ; i<array.length ; i++)
{
System.out.print(array[i]+" ");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int []array = {4,7,3,1,0,9,10,2};
System.out.println("Before____________");
// print result
for(int i=0 ; i<array.length ; i++)
{
System.out.print(array[i]+" ");
}
System.out.println("\nAfter____________");
Sort(array,array.length);
}
}