-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSortUtil.java
154 lines (141 loc) · 3.29 KB
/
SortUtil.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
public class SortUtil {
public static void insertionSort(int[] iList) {
int len = iList.length;
for (int i = 1; i < len; i++) {
int cur = iList[i];
int j = i;
while (j > 0 && cur < iList[j-1]) {
iList[j] = iList[j-1];
j--;
}
iList[j] = cur;
}
}
public static void selectionSort(int[] iList) {
int len = iList.length;
for (int i = 0; i < len-1; i++) {
int minIndex = i;
for (int j = i+1; j < len; j++) {
if (iList[j] < iList[minIndex]) {
minIndex = j;
}
}
int temp = iList[i];
iList[i] = iList[minIndex];
iList[minIndex] = temp;
}
}
public static void bubbleSort(int[] iList) {
int len = iList.length;
for (int i = len-1; i >= 1; i--) {
boolean isSorted = true;
for (int j = 0; j <= i-1; j++) {
if (iList[j] > iList[j+1]) {
int temp = iList[j];
iList[j] = iList[j+1];
iList[j+1] = temp;
isSorted = false;
}
}
if (isSorted) {
return;
}
}
}
public static void mergeSort(int[] iList) {
int len = iList.length;
int[] curList = iList;
int[] cpList = new int[len];
for (int i = 1; i < len; i *= 2) {
for (int j = 0; j < len; j += 2*i) {
int bound1 = Math.min(j+i-1, len-1);
int bound2 = Math.min(j+2*i-1, len-1);
int position1 = j;
int position2 = j+i;
int position3 = j;
while (position1 <= bound1 || position2 <= bound2) {
if (position1 > bound1) {
while (position2 <= bound2) {
cpList[position3++] = curList[position2++];
}
break;
} else if (position2 > bound2) {
while (position1 <= bound1) {
cpList[position3++] = curList[position1++];
}
break;
} else {
if (curList[position1] < curList[position2]) {
cpList[position3++] = curList[position1++];
} else {
cpList[position3++] = curList[position2++];
}
}
}
}
int[] temp = curList;
curList = cpList;
cpList = temp;
}
iList = curList;
}
public static void quickSort(int[] iList) {
quickSortHelper(iList, 0, iList.length);
}
private static void quickSortHelper(int[] iList, int start, int len) {
if (len <= 1) {
return;
}
int more = start+len-1;
int less = start;
while (more > less) {
if (iList[more] <= iList[more-1]) {
int temp = iList[more-1];
iList[more-1] = iList[more];
iList[more] = temp;
more--;
} else {
int temp = iList[less];
iList[less] = iList[more-1];
iList[more-1] = temp;
less++;
}
}
quickSortHelper(iList, start, more-start);
quickSortHelper(iList, more+1, start+len-more-1);
}
public static void heapSort(int[] iList) {
int len = iList.length;
for (int i = 1; i < len; i++) {
int j = i;
while (j >= 1 && iList[j] > iList[(j-1)/2]) {
int temp = iList[j];
iList[j] = iList[(j-1)/2];
iList[(j-1)/2] = temp;
j = (j-1)/2;
}
}
for (int i = len-1; i >= 1;i--) {
int temp = iList[0];
iList[0] = iList[i];
iList[i] = temp;
int j = 0;
while (j*2+1 <= i-1) {
int maxChild = 0;
if (j*2+2 <= i-1) {
maxChild = iList[j*2+1] > iList[j*2+2]?j*2+1:j*2+2;
} else {
maxChild = j*2+1;
}
if (iList[maxChild] > iList[j]) {
int temp1 = iList[j];
iList[j] = iList[maxChild];
iList[maxChild] = temp1;
j = maxChild;
} else {
break;
}
}
}
}
}