-
Notifications
You must be signed in to change notification settings - Fork 14
/
SortUtils.java
226 lines (201 loc) · 4.9 KB
/
SortUtils.java
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
225
226
package com.onlylemi.javareview.sort;
/**
* SortUtils
*
* @author: onlylemi
* @time: 2016-07-24 10:08
*/
public class SortUtils {
private SortUtils() {
}
/**
* 插入排序
*
* @param a
*/
public static void insertSort(int[] a) {
for (int i = 0; i < a.length; i++) {
// 待插入元素
int temp = a[i];
int j = i - 1;
while (j >= 0 && temp < a[j]) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = temp;
}
}
/**
* 希尔排序
*
* @param a
*/
public static void shellSort(int[] a) {
int d = a.length;
while (true) {
// 分块
d = d / 2;
for (int i = 0; i < d; i++) {
// 每部分进行插入排序
for (int j = i; j < a.length; j += d) {
int temp = a[j];
int k = j - d;
while (k >= 0 && temp < a[k]) {
a[k + d] = a[k];
k -= d;
}
a[k + d] = temp;
}
}
if (d == 1) {
break;
}
}
}
/**
* 堆排序
*
* @param a
*/
public static void heapSort(int[] a) {
int i = (a.length - 1) / 2;
while (i >= 0) {
maxHeap(a, i, a.length);
i--;
}
for (i = a.length - 1; i >= 0; i--) {
int temp = a[0];
a[0] = a[i];
a[i] = temp;
maxHeap(a, 0, i);
}
}
private static void maxHeap(int[] a, int i, int size) {
int left = 2 * i + 1;
int right = 2 * i + 2;
int large = i;
if (left < size && a[large] < a[left]) {
large = left;
}
if (right < size && a[large] < a[right]) {
large = right;
}
if (large != i) {
int temp = a[i];
a[i] = a[large];
a[large] = temp;
maxHeap(a, large, size);
}
}
/**
* 选择排序
*
* @param a
*/
public static void selectSort(int[] a) {
for (int i = 0; i < a.length; i++) {
int min = i;
for (int j = i; j < a.length; j++) {
if (a[min] > a[j]) {
min = j;
}
}
// 找到最小元素的下标,与首元素交换
if (min != i) {
int temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
}
/**
* 冒泡排序
*
* @param a
*/
public static void bubbleSort(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - 1 - i; j++) {
// 大的往下沉,小的往上浮
if (a[j] > a[j + 1]) {
int temp = a[j + 1];
a[j + 1] = a[j];
a[j] = temp;
}
}
}
}
/**
* 快速排序
*
* @param a
*/
public static void quickSort(int[] a) {
quickSort(a, 0, a.length - 1);
}
private static void quickSort(int[] a, int left, int right) {
if (left > right) {
return;
}
int i = left;
int j = right;
int key = a[left]; // 选取第一个元素为基准
while (i < j) {
while (i < j && key <= a[j]) {
j--;
}
a[i] = a[j];
while (i < j && key >= a[i]) {
i++;
}
a[j] = a[i];
}
a[i] = key;
quickSort(a, left, i - 1);
quickSort(a, i + 1, right);
}
/**
* 归并排序
*
* @param a
*/
public static void mergeSort(int[] a) {
mergeSort(a, 0, a.length - 1);
}
private static void mergeSort(int[] a, int left, int right) {
if (left >= right) {
return;
}
int middle = (left + right) / 2;
mergeSort(a, left, middle); // 左
mergeSort(a, middle + 1, right); // 右
merge(a, left, middle, right); //合并
}
private static void merge(int[] a, int left, int middle, int right) {
int i = left;
int j = middle + 1;
int[] b = new int[right - left + 1];
int k = 0;
while (i <= middle && j <= right) {
if (a[i] <= a[j]) {
b[k] = a[i];
i++;
} else {
b[k] = a[j];
j++;
}
k++;
}
while (i <= middle) {
b[k] = a[i];
i++;
k++;
}
while (j <= right) {
b[k] = a[j];
j++;
k++;
}
System.arraycopy(b, 0, a, left, k);
}
}