-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
29 lines (27 loc) · 885 Bytes
/
Copy pathSolution.java
File metadata and controls
29 lines (27 loc) · 885 Bytes
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
// Stepsort · Shell Sort
// Category: Sorting
// Animated walkthrough: https://stepsort.prakashraj.me/algorithm/shell-sort
public class Main {
// Gapped insertion sorts with a gap that halves every pass.
static void shellSort(int[] arr) {
int n = arr.length;
for (int gap = n / 2; gap > 0; gap /= 2) {
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;
}
}
}
public static void main(String[] args) {
int[] data = {12, 34, 54, 2, 3};
shellSort(data);
System.out.print("sorted:");
for (int x : data) System.out.print(" " + x);
System.out.println();
}
}