-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsort.java
More file actions
252 lines (232 loc) · 4.37 KB
/
sort.java
File metadata and controls
252 lines (232 loc) · 4.37 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package sort;
public class sort {
// 插入排序
public static void insertSort(int p[]) {
if (p == null || p.length <= 0) {
return;
}
int tmp;
for (int i = 1; i < p.length; i++) {
for (int j = i; j > 0; j--) {
if (p[j] < p[j - 1]) {
tmp = p[j];
p[j] = p[j - 1];
p[j - 1] = tmp;
}
}
}
}
// 希尔排序
public static void shellSort(int p[]) {
if (p == null || p.length <= 0) {
return;
}
int length = p.length / 2;
int temp;
while (length != 0) {
for (int i = 0; i < p.length; i++) {
for (int j = i; j >= length; j -= length) {
if (p[j] < p[j - length]) {
temp = p[j];
p[j] = p[j - length];
p[j - length] = temp;
}
}
}
length = length / 2;
}
}
// 选择排序
public static void selectSort(int p[]) {
if (p == null || p.length <= 0) {
return;
}
int temp;
for (int i = 0; i < p.length - 1; i++) {
int index = i;
for (int j = i + 1; j < p.length; j++) {
if (p[j] < p[index]) {
index = j;
}
}
if (index != i) {
temp = p[i];
p[i] = p[index];
p[index] = temp;
}
}
}
// 冒泡排序
public static void bubbleSort(int p[]) {
if (p == null || p.length <= 0) {
return;
}
int temp;
for (int i = 0; i < p.length - 1; i++) {
for (int j = 0; j < p.length - i - 1; j++) {
if (p[j] > p[j + 1]) {
temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
}
// 计数排序
public static int[] countSort(int p[]) {
if (p == null || p.length <= 0) {
return null;
}
int count = 0;
int q[] = new int[p.length];
for (int i = 0; i < p.length; i++) {
for (int j = 0; j < p.length; j++) {
if (p[i] > p[j]) {
count++;
}
}
q[count] = p[i];
count = 0;
}
return q;
}
// 基数排序(获取位数字)
public static int getDigit(int x, int d) {
int n[] = { 1, 10, 100 };
return x / n[d] % 10;
}
// 基数排序
public static void radixSort(int p[], int d) {
if (p == null || p.length <= 0) {
return;
}
int index;
int temp[] = new int[p.length];
int count[] = new int[10];
for (int i = 0; i < d; i++) {
for (int j = 0; j < 10; j++) {
count[j] = 0;
}
for (int j = 0; j < p.length; j++) {
index = getDigit(p[j], i);
count[index]++;
}
for (int j = 1; j < 10; j++) {
count[j] = count[j] + count[j - 1];
}
for (int m = p.length - 1; m >= 0; m--) {
index = getDigit(p[m], i);
temp[count[index] - 1] = p[m];
count[index]--;
}
for (int m = 0; m < p.length; m++) {
p[m] = temp[m];
}
}
}
// 归并排序
public static void mergeSort(int p[], int start, int end) {
if (p == null || p.length <= 0) {
return;
}
if (end == start) {
return;
}
int middle = (start + end) / 2;
int temp[] = new int[p.length];
int i, j, t;
mergeSort(p, start, middle);
mergeSort(p, middle + 1, end);
i = start;
j = middle + 1;
t = start;
while (i <= middle && j <= end) {
if (p[i] < p[j]) {
temp[t] = p[i];
i++;
t++;
} else {
temp[t] = p[j];
j++;
t++;
}
}
while (i <= middle) {
temp[t] = p[i];
i++;
t++;
}
while (j <= end) {
temp[t] = p[j];
j++;
t++;
}
for (int m = start; m <= end; m++) {
p[m] = temp[m];
}
}
// 快速排序
public static void quickSort(int p[], int start, int end) {
if (p == null || p.length <= 0) {
return;
}
if (start < end) {
int key = p[start];
int l = start + 1;
int r = end;
int temp;
while (l <= r) {
while (l <= r && p[l] < key) {
l++;
}
while (l <= r && p[r] > key) {
r--;
}
if (l <= r) {
temp = p[l];
p[l] = p[r];
p[r] = temp;
l++;
r--;
}
}
temp = p[r];
p[r] = p[start];
p[start] = temp;
quickSort(p, start, r - 1);
quickSort(p, r + 1, end);
}
}
// 堆排序(调整堆)
public static void adjustHeap(int p[], int i, int length) {
int temp = p[i];
for (int k = 2 * i + 1; k < length; k = 2 * k + 1) {
if (k + 1 < length && p[k] < p[k + 1]) {
k++;
}
if (p[k] > temp) {
p[i] = p[k];
i = k;
} else {
break;
}
}
p[i] = temp;
}
// 堆排序
public static void heapSort(int p[]) {
if (p == null || p.length <= 0) {
return;
}
int temp;
for (int i = p.length / 2 - 1; i >= 0; i--) {
adjustHeap(p, i, p.length);
}
for (int i = p.length - 1; i > 0; i--) {
temp = p[0];
p[0] = p[i];
p[i] = temp;
adjustHeap(p, 0, i);
}
}
}