forked from Annex5061/java-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyClass.java
33 lines (31 loc) · 1020 Bytes
/
MyClass.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
/*Shell sort addresses this problem and reduces the number of shifts/swaps by dividing the array into subarrays of intervals (gap) and then applying insertion sort on
the sub-arrays. This process is repeated with reducing interval (gap) size until the gap becomes 0.
As a result, the number of swaps significantly reduces but at the cost of more number of comparisons.*/
public class MyClass {
public static void main(String args[]) {
int arr[] = {4,2,8,4,6,1};
shellSort(arr);
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
}
public static void shellSort(int arr[])
{
int n = arr.length;
int gap = n/2;
while(gap>0)
{
for(int i=gap;i<n;i++)
{
int temp = arr[i];
int j=i;
while(j>=gap && arr[j-gap]>temp)
{
arr[j] = arr[j-gap];
j -= gap;
}
arr[j] = temp;
}
gap = gap/2;
}
}
}