0%

排序算法总结

直接插入排序、希尔排序、冒泡排序、快速排序、简单选择排序、堆排序、归并排序的实现。

排序算法性能比较

3DMK0g.jpg

补充:

  • 稳定排序算法:插入排序、冒泡排序、归并排序、基数排序
  • 不稳定排序算法:希尔排序、快速排序、选择排序、堆排序

直接插入排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* 直接插入排序
*
* @param array
*/
public static void insertSort(int[] array) {
for (int i = 1, length = array.length; i < length; i++) {
int temp = array[i];
int j;
for (j = i - 1; j >= 0 && temp < array[j]; j--) {
array[j + 1] = array[j];
}
array[j + 1] = temp;
}
}

希尔排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* 希尔排序
*
* @param array
*/
public static void shellSort(int[] array) {
int length = array.length;
for (int d = length / 2; d >= 1; d /= 2) {
for (int i = d; i < length; i++) {
int temp = array[i];
int j;
for (j = i - d; j >= 0 && temp < array[j]; j -= d) {
array[j + d] = array[j];
}
array[j + d] = temp;
}
}
}

冒泡排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* 冒泡排序
*
* @param array
*/
public static void bubbleSort(int[] array) {
int length = array.length;
for (int i = 0; i < length; i++) {
boolean exchange = false;
for (int j = 0; j < length - i - 1; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
exchange = true;
}
}
if (exchange == false) {
break;
}
}
}

快速排序

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
40
41
42
43
44
/**
* 快速排序一次划分算法
*
* @param array
* @param left
* @param right
* @return
*/
private static int partition(int[] array, int left, int right) {
int i = left, j = right, base = array[left];
while (i < j) {
while (i < j && array[j] > base) {
j--;
}
if (i < j) {
array[i] = array[j];
i++;
}
while (i < j && array[i] < base) {
i++;
}
if (i < j) {
array[j] = array[i];
j--;
}
}
array[i] = base;
return i;
}

/**
* 快速排序
*
* @param array
* @param left
* @param right
*/
public static void quickSort(int[] array, int left, int right) {
if (left < right) {
int pivot = partition(array, left, right);
quickSort(array, left, pivot - 1);
quickSort(array, pivot + 1, right);
}
}

简单选择排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* 简单选择排序
*
* @param array
*/
public static void selectSort(int[] array) {
int length = array.length;
for (int i = 0; i < length - 1; i++) {
int index = i;
for (int j = i + 1; j < length; j++) {
if (array[j] < array[index]) {
index = j;
}
}
if (index != i) {
int temp = array[i];
array[i] = array[index];
array[index] = temp;
}
}
}

推排序

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
40
41
42
/**
* 调整成大根堆
*
* @param array
* @param k 当前要筛选的节点
* @param len 数组无序区长度
*/
private static void heapAdjust(int[] array, int k, int len) {
int i = k, j = 2 * k + 1;
while (j < len) {
if (j < len - 1 && array[j] < array[j + 1]) {
j++;
}
if (array[i] > array[j]) {
break;
} else {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
i = j;
j = 2 * i + 1;
}
}
}

/**
* 堆排序
*
* @param array
*/
public static void heapSort(int[] array) {
int length = array.length;
for (int i = length / 2; i >= 0; i--) {
heapAdjust(array, i, length);
}
for (int i = length - 1; i > 0; i--) {
int temp = array[i];
array[i] = array[0];
array[0] = temp;
heapAdjust(array, 0, i);
}
}

归并排序

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
40
41
42
43
44
/**
* 一次归并算法
*
* @param array
* @param start
* @param mid
* @param end
*/
private static void merge(int[] array, int left, int mid, int right) {
int i = left, j = mid + 1, k = 0;
int[] temp = new int[right - left + 1];
while (i <= mid && j <= right) {
if (array[i] < array[j]) {
temp[k++] = array[i++];
} else {
temp[k++] = array[j++];
}
}
while (i <= mid) {
temp[k++] = array[i++];
}
while (j <= right) {
temp[k++] = array[j++];
}
for (int p = 0, length = temp.length; p < length; p++) {
array[left + p] = temp[p];
}
}

/**
* 归并排序
*
* @param array
* @param left
* @param right
*/
public static void mergeSort(int[] array, int left, int right) {
if (left < right) {
int mid = (left + right) / 2;
mergeSort(array, left, mid);
mergeSort(array, mid + 1, right);
merge(array, left, mid, right);
}
}

汇总

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import java.util.Arrays;

public class ArraySorts {

/**
* 直接插入排序
*
* @param array
*/
public static void insertSort(int[] array) {
for (int i = 1, length = array.length; i < length; i++) {
int temp = array[i];
int j;
for (j = i - 1; j >= 0 && temp < array[j]; j--) {
array[j + 1] = array[j];
}
array[j + 1] = temp;
}
}

/**
* 希尔排序
*
* @param array
*/
public static void shellSort(int[] array) {
int length = array.length;
for (int d = length / 2; d >= 1; d /= 2) {
for (int i = d; i < length; i++) {
int temp = array[i];
int j;
for (j = i - d; j >= 0 && temp < array[j]; j -= d) {
array[j + d] = array[j];
}
array[j + d] = temp;
}
}
}

/**
* 冒泡排序
*
* @param array
*/
public static void bubbleSort(int[] array) {
int length = array.length;
for (int i = 0; i < length; i++) {
boolean exchange = false;
for (int j = 0; j < length - i - 1; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
exchange = true;
}
}
if (exchange == false) {
break;
}
}
}

/**
* 快速排序一次划分算法
*
* @param array
* @param left
* @param right
* @return
*/
private static int partition(int[] array, int left, int right) {
int i = left, j = right, base = array[left];
while (i < j) {
while (i < j && array[j] > base) {
j--;
}
if (i < j) {
array[i] = array[j];
i++;
}
while (i < j && array[i] < base) {
i++;
}
if (i < j) {
array[j] = array[i];
j--;
}
}
array[i] = base;
return i;
}

/**
* 快速排序
*
* @param array
* @param left
* @param right
*/
public static void quickSort(int[] array, int left, int right) {
if (left < right) {
int pivot = partition(array, left, right);
quickSort(array, left, pivot - 1);
quickSort(array, pivot + 1, right);
}
}

/**
* 简单选择排序
*
* @param array
*/
public static void selectSort(int[] array) {
int length = array.length;
for (int i = 0; i < length - 1; i++) {
int index = i;
for (int j = i + 1; j < length; j++) {
if (array[j] < array[index]) {
index = j;
}
}
if (index != i) {
int temp = array[i];
array[i] = array[index];
array[index] = temp;
}
}
}

/**
* 调整成大根堆
*
* @param array
* @param k 当前要筛选的节点
* @param len 数组无序区长度
*/
private static void heapAdjust(int[] array, int k, int len) {
int i = k, j = 2 * k + 1;
while (j < len) {
if (j < len - 1 && array[j] < array[j + 1]) {
j++;
}
if (array[i] > array[j]) {
break;
} else {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
i = j;
j = 2 * i + 1;
}
}
}

/**
* 堆排序
*
* @param array
*/
public static void heapSort(int[] array) {
int length = array.length;
for (int i = length / 2; i >= 0; i--) {
heapAdjust(array, i, length);
}
for (int i = length - 1; i > 0; i--) {
int temp = array[i];
array[i] = array[0];
array[0] = temp;
heapAdjust(array, 0, i);
}
}

/**
* 一次归并算法
*
* @param array
* @param start
* @param mid
* @param end
*/
private static void merge(int[] array, int left, int mid, int right) {
int i = left, j = mid + 1, k = 0;
int[] temp = new int[right - left + 1];
while (i <= mid && j <= right) {
if (array[i] < array[j]) {
temp[k++] = array[i++];
} else {
temp[k++] = array[j++];
}
}
while (i <= mid) {
temp[k++] = array[i++];
}
while (j <= right) {
temp[k++] = array[j++];
}
for (int p = 0, length = temp.length; p < length; p++) {
array[left + p] = temp[p];
}
}

/**
* 归并排序
*
* @param array
* @param left
* @param right
*/
public static void mergeSort(int[] array, int left, int right) {
if (left < right) {
int mid = (left + right) / 2;
mergeSort(array, left, mid);
mergeSort(array, mid + 1, right);
merge(array, left, mid, right);
}
}

public static void main(String[] args) {
int[] array = { 12, 15, 9, 20, 6, 31, 24, 15 };
// ArraySorts.mergeSort(array, 0, array.length - 1);
ArraySorts.heapSort(array);
System.out.println(Arrays.toString(array));
}
}