-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort.java
More file actions
34 lines (31 loc) · 923 Bytes
/
ShellSort.java
File metadata and controls
34 lines (31 loc) · 923 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
package algorithms.sort;
/**
* Created : zzc
* Time : 2017/9/26
* Email : zzcm159@gmail.com
* Description :希尔排序
*/
public class ShellSort {
public static void main(String arg[]) {
int[] arr = ArrUtils.randomArr(30);
int[] arrSorted = shellSort(arr);
for (int a = 0; a < arrSorted.length; a++) {
System.out.println(arrSorted[a]);
}
}
public static int[] shellSort(int arr[]) {
if (arr != null && arr.length > 1) {
int j;
for (int gap = arr.length / 2; gap > 0; gap /= 2) {
for (int i = gap; i < arr.length; i++) {
int temp = arr[i];
for (j = i; j >= gap && temp < arr[j - gap]; j -= gap) {
arr[j] = arr[j - gap];
}
arr[j] = temp;
}
}
}
return arr;
}
}