Skip to content

Commit 5ccc1d5

Browse files
committed
添加希尔排序实现
1 parent a5d1ccf commit 5ccc1d5

File tree

7 files changed

+112
-19
lines changed

7 files changed

+112
-19
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.mistray.insertion;
2+
3+
import com.mistray.model.SortModel;
4+
5+
/**
6+
* @author MistRay
7+
* @create 2018-11-19
8+
* @desc 希尔排序
9+
* 逻辑:  希尔排序是把记录按下标的一定增量分组,对每组使用直接插入排序算法排序;
10+
* 随着增量逐渐减少,每组包含的关键词越来越多,当增量减至1时,整个文件恰被分成一组,算法便终止。
11+
* 时间复杂度:
12+
* 平均情况:O(n^1.3)
13+
* 最好情况:O(n)
14+
* 最坏情况:O(n^2)
15+
* 空间复杂度:O(1)
16+
* 稳定性:不稳定
17+
*/
18+
public class ShellSort {
19+
20+
public static void main(String[] args) {
21+
Integer[] a = {5, 1, 9, 22, 15, 3, 8};
22+
ShellSort.shellSort(a);
23+
System.out.println("==================================");
24+
Integer[] b = {3, 6, 1, 13, 2, 32, 15};
25+
ShellSort.shellSort2(b);
26+
}
27+
28+
29+
public static void shellSort(Comparable[] a) {
30+
// 增量gap,并逐步缩小增量
31+
for (int gap = a.length; gap > 0; gap /= 2) {
32+
// 从第gap个元素逐个对其所在组进行插入排序操作
33+
for (int i = gap; i < a.length; i++) {
34+
int j = i;
35+
// 当a[j]和a[j-gap]
36+
while (j - gap >= 0 && SortModel.less(a[j], a[j - gap])) {
37+
// 交换两个元素的位置
38+
SortModel.exch(a, j, j - gap);
39+
j -= gap;
40+
}
41+
SortModel.show(a);
42+
}
43+
44+
SortModel.show(a);
45+
}
46+
}
47+
48+
public static void shellSort2(Comparable[] a) {
49+
// 将a[]按升序排序
50+
int N = a.length;
51+
int h = 1;
52+
while (h < N / 3) {
53+
// 1,4,13,40,121,364,1093
54+
h = 3 * h + 1;
55+
}
56+
while (h >= 1) {
57+
// 将数组变为h有序
58+
for (int i = h; i < N; i++) {
59+
// 将a[i] 插入到a[i-h],a[i-2*h],a[i-3*h]..之中
60+
for (int j = i; j >= h && SortModel.less(a[j], a[j - h]); j -= h) {
61+
SortModel.exch(a, j, j - h);
62+
}
63+
}
64+
h = h / 3;
65+
}
66+
SortModel.show(a);
67+
}
68+
69+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.mistray.merge;
2+
3+
/**
4+
* @author MistLight
5+
* @create 2018-11-20
6+
* @desc 归并排序
7+
*/
8+
public class MergeSort {
9+
10+
public static void main(String[] args) {
11+
12+
}
13+
14+
public static void mergeSort(){
15+
16+
}
17+
}

sort/src/main/java/com/mistray/model/SortModel.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
*/
1010
public class SortModel {
1111

12+
/**
13+
*
14+
* @param v
15+
* @param w
16+
* @return
17+
* true v < m
18+
* false v > m
19+
*/
1220
public static boolean less(Comparable v, Comparable w) {
1321
return v.compareTo(w) < 0;
1422
}

sort/src/main/java/com/mistray/quicksort/QuickSort.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

sort/src/main/java/com/mistray/selection/Selection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @create 2018-11-16
88
* @desc 选择排序
99
*
10-
* 逻辑:遍历数组,找到最小的值放在最左边
10+
* 逻辑:遍历数组,找到最小的值与最左边的元素替换
1111
* 时间复杂度
1212
* 平均情况:O(n^2)
1313
* 最好情况:O(n^2)

sort/src/main/java/com/mistray/shell/ShellSort.java

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.mistray.swapsort;
2+
3+
/**
4+
* @author MistLight
5+
* @create 2018-11-19
6+
* @desc 快速排序
7+
*/
8+
public class QuickSort {
9+
public static void main(String[] args) {
10+
11+
12+
}
13+
14+
public static void quickSort(Integer[] quc){
15+
16+
}
17+
}

0 commit comments

Comments
 (0)