-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSort.java
232 lines (213 loc) · 4.48 KB
/
Sort.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
227
228
229
230
231
232
public class Sort {
private int comparisons;
private int exchanges;
public Sort() {
comparisons = 0;
exchanges = 0;
}
public int getComparisons() {
return(comparisons);
}
public int getExchanges() {
return(exchanges);
}
private void reset() {
comparisons = 0;
exchanges = 0;
}
public void bubbleSort(AnyType[] array) {
AnyType temp;
int n = array.length;
boolean cont = true;
reset();
for(int i = 0; i < n; i++) {
if(cont) {
cont = false;
for(int j = 1; j < n - i; j++) {
comparisons++;
if(array[j-1].isBetterThan(array[j])) {
exchanges++;
temp = array[j];
array[j-1] = array[j];
array[j] = temp;
cont = true;
}
}
}
else {
break;
}
}
}
public void insertionSort (AnyType[] array) {
int n = array.length;
reset();
for(int i = 1; i < n; i++) {
int j = i;
AnyType x = array[i];
while(j > 0) {
comparisons++;
if(array[j-1].isBetterThan(x)) {
exchanges++;
array[j] = array[j-1];
j--;
}
else {
break;
}
}
array[j] = x;
}
}
public void selectionSort (AnyType[] array) {
int n = array.length;
reset();
for(int i = 0; i < n - 1; i++) {
int min = i;
for(int j = i+1; j < n; j++) {
comparisons++;
if(array[min].isBetterThan(array[j])) {
min = j;
}
}
exchanges++;
AnyType temp = array[min];
array[min] = array[i];
array[i] = temp;
}
}
private int[] sequence1(int n) {
int[] result = new int[n];
int i = 0;
do {
result[i] = n / (int) Math.pow(2, i + 1);
} while (result[i++] > 1);
return result;
}
private int[] sequence2(int n) {
int[] result = new int[n];
int i = 0;
do {
result[i] = 2 * (n / (int) Math.pow(2, i + 2)) + 1;
} while (result[i++] > 1);
return result;
}
private int[] sequence3(int n) {
int[] result = new int[n];
int i = 0;
do {
result[i] = (int) Math.pow(2, i + 1) - 1;
} while ((int) Math.pow(2, ++i + 1) - 1 < n);
return result;
}
private int[] sequence4(int n) {
int[] result = new int[n];
int i = 0;
do {
result[i] = (int) (Math.pow(3, i + 1) - 1) / 2;
} while ((int) (Math.pow(3, ++i + 1) - 1) / 2 < n);
return result;
}
public void shellSort(AnyType[] array) {
reset();
int n = array.length;
int[] seq = sequence4(n);
for(int h = 0; seq[h] > 0; h++) {
for (int i = seq[h]; i < n; i++) {
int j = i;
AnyType temp = array[i];
while (j >= seq[h]) {
comparisons++;
if(array[j - seq[h]].isBetterThan(temp)) {
exchanges++;
array[j] = array[j - seq[h]];
j = j - seq[h];
} else {
break;
}
}
array[j] = temp;
}
}
}
private void quickSortRec(AnyType[] array, int lo, int hi) {
int i = lo;
int j = hi;
AnyType pivot = array[lo + (hi - lo) / 2];
while(i <= j) {
comparisons++;
while(pivot.isBetterThan(array[i]))
i++;
while(array[j].isBetterThan(pivot))
j--;
if(i <= j) {
exchanges++;
AnyType temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
}
if(lo < j)
quickSortRec(array, lo, j);
if(i < hi)
quickSortRec(array, i, hi);
}
public void quickSort(AnyType[] array) {
reset();
quickSortRec(array, 0, array.length - 1);
}
public void heapSort(AnyType[] array) {
int i, j, leftChild, rightChild, betterChild, root;
AnyType temp;
int n = array.length - 1;
reset();
root = (n-1)/2;
for(j = root; j >= 0; j--) {
for(i = root; i >= 0; i--) {
leftChild = (2*i)+1;
rightChild = (2*i)+2;
if((leftChild <= n) && (rightChild <= n)) {
comparisons++;
if(array[rightChild].isBetterThan(array[leftChild]))
betterChild = rightChild;
else
betterChild = leftChild;
}
else{
if(rightChild > n)
betterChild = leftChild;
else
betterChild = rightChild;
}
if(array[betterChild].isBetterThan(array[i])){
exchanges++;
comparisons++;
temp = array[i];
array[i] = array[betterChild];
array[betterChild] = temp;
}
}
}
temp = array[0];
array[0] = array[n];
array[n] = temp;
}
// private void upheap(AnyType[] array, int j) {
// int l = j;
// int key = array[l];
// int k = l/2;
// while(k>=1) {
// comparisons++;
// if(key.isBetterThan(array[k])) {
// array[l] = array[k];
// l = k;
// k = l/2;
// }
// }
// }
// private void insertItem(AnyType[] H, AnyType Item) {
// if(
// }
}