-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathshell_sort.java
34 lines (31 loc) · 1.23 KB
/
shell_sort.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
import java.util.Arrays;
class shell_sort {
public static void shellSort(int[] arr) {
int n = arr.length;
// Start with a big gap, then reduce the gap
for (int gap = n / 2; gap > 0; gap /= 2) {
// Do a gapped insertion sort for this gap size.
// The first gap elements a[0..gap-1] are already in gapped order.
// Keep adding one more element until the entire array is gap sorted.
for (int i = gap; i < n; i++) {
int temp = arr[i];
int j = i;
while (j >= gap && arr[j - gap] > temp) {
// Shift earlier gap-sorted elements up until the correct location for a[i] is found.
arr[j] = arr[j - gap];
j -= gap;
}
// Put temp (the original a[i]) in its correct location.
arr[j] = temp;
}
}
}
public static void main(String[] args) {
int[] arr = {9, 8, 1, 7, 4, 3};
System.out.println("Array before sorting:");
System.out.println(Arrays.toString(arr));
shellSort(arr);
System.out.println("Array after sorting:");
System.out.println(Arrays.toString(arr));
}
}