-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathShellSort.java
More file actions
30 lines (20 loc) · 658 Bytes
/
Copy pathShellSort.java
File metadata and controls
30 lines (20 loc) · 658 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
30
// Shell Sort in Java
import java.util.Arrays;
public class ShellSort {
public static void main(String[] args) {
int[] nums = {50, 34, 78, 44, 8, 90, 21, 1, 30};
int n = nums.length;
int temp;
int j;
for (int gap = n/2; gap >= 1; gap /= 2) {
for (int i = gap; i < n; i++) {
temp = nums[i];
for (j = i; j >= gap && nums[j-gap] > temp; j -= gap) {
nums[j] = nums[j - gap];
}
nums[j] = temp;
}
}
System.out.println(Arrays.toString(nums));
}
}