-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortUtil.java
More file actions
39 lines (33 loc) · 849 Bytes
/
SortUtil.java
File metadata and controls
39 lines (33 loc) · 849 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
31
32
33
34
35
36
37
38
39
package code.algorithm.sort;
public class SortUtil {
public static <T extends Comparable<? super T>> void insertSort(T[] input) {
}
public static void insertSort(int[] nums) {
int j;
for (int i = 1; i < nums.length; i++) {
int cur = nums[i];
for (j = i; j > 0 && cur < nums[j - 1]; j--) {
nums[j] = nums[j - 1];
}
nums[j] = cur;
}
}
public static void shellSort(int[] nums) {
int j;
for (int gap = nums.length / 2; gap > 0; gap /= 2) {
for (int i = gap; i < nums.length; i++) {
int cur = nums[i];
for (j = i; j >= gap && cur < nums[j - gap]; j -= gap) {
nums[j] = nums[j - gap];
}
nums[j] = cur;
}
}
}
public static void main(String[] args) {
// int[] input = { 9, 3, 2, 4, 8 };
int[] input = { 1, 2, 3, 4, 5, 6, 8, 9, 10, 7 };
// insertSort(input);
shellSort(input);
}
}