-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.c
290 lines (248 loc) · 5.84 KB
/
sort.c
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
This is a group of sorting algorithms
Author: Roger Barker
Last Edit: 20150831
*/
#include "sort.h"
//------------------------------------------------------------
//Private Definitions
//------------------------------------------------------------
/*
(Put elements of 'a' in heap order, in-place)
procedure heapify(a, count) is
(start is assigned the index in 'a' of the last parent node)
(the last element in a 0-based array is at index count-1; find the parent of that element)
start ← floor ((count - 2) / 2)
while start ≥ 0 do
(sift down the node at index 'start' to the proper place such that all nodes below
the start index are in heap order)
siftDown(a, start, count - 1)
(go to the next parent node)
start ← start - 1
(after sifting down the root all nodes/elements are in heap order)
*/
void heapify( int* arrayToSort, int size );
/*
(Repair the heap whose root element is at index 'start', assuming the heaps rooted at its children are valid)
procedure siftDown(a, start, end) is
root ← start
while root * 2 + 1 ≤ end do (While the root has at least one child)
child ← root * 2 + 1 (Left child)
swap ← root (Keeps track of child to swap with)
if a[swap] < a[child]
swap ← child
(If there is a right child and that child is greater)
if child+1 ≤ end and a[swap] < a[child+1]
swap ← child + 1
if swap = root
(The root holds the largest element. Since we assume the heaps rooted at the
children are valid, this means that we are done.)
return
else
swap(a[root], a[swap])
root ← swap (repeat to continue sifting down the child now)
*/
void siftDown( int* arrayToSort, int start, int end );
/*
Partition(A,p,r):
x = A[r]
i = p - 1
for j = p to r - 1 do
if A[j] <= x
i++
swap A[i] and A[j]
swap A[i+1] with A[r]
return i+1
*/
int partition( int* A, int p, int r);
/*
merge(A,p,q,r):
n1 <- q - p + 1
n2 <- r - q
for i<-1 to n1 do
L[i] <- A[p+i-1]
for j<-1 to n2 do
R[j] <- A[q+j]
i = 1, j = 1
for k<-p to r do
if L[i] <= R[j]
then A[k] = L[i]
i <- i+1
else
then A[k] = R[j]
j <- j+1
*/
void merge( int* arrayToSort, int lhv, int mv, int rhv );
//------------------------------------------------------------
//End Definitions
//------------------------------------------------------------
void selectionSort(int* arrayToSort, int size ){
int i = 0;
int j = 0;
for(i = 0; i < size; i++){
int min = i;
int temp = 0;
for(j = i+1; j < size; j++){
if(arrayToSort[j] < arrayToSort[min]){
min = j;
}
}
temp = arrayToSort[i];
arrayToSort[i] = arrayToSort[min];
arrayToSort[min] = temp;
}
}
void insertionSort( int* arrayToSort, int size ){
int i = 0;
int j = 0;
int key = 0;
for( j = 1; j < size; j++ ){
key = arrayToSort[j];
i = j - 1;
while(i >= 0 && key < arrayToSort[i]){
arrayToSort[i+1] = arrayToSort[i];
i = i - 1;
}
arrayToSort[i+1] = key;
}
}
void mergeSort( int* arrayToSort, int p, int r ){
if(p < r){
int q = p + (r-p)/2;
mergeSort(arrayToSort,p,q);
mergeSort(arrayToSort,q+1,r);
merge(arrayToSort,p,q,r);
}
}
void merge( int* A, int l, int m, int r ){
int i,j,k;
int n1 = m-l+1;
int n2 = r-m;
int L[n1], R[n2];
for(i = 0; i < n1; i++)
L[i] = A[l+i];
for(j = 0; j < n2; j++)
R[j] = A[m+1+j];
i = 0;
j = 0;
k = l;
while( i < n1 && j < n2 ){
if( L[i] <= R[j] )
A[k] = L[i++];
else
A[k] = R[j++];
k++;
}
while( i < n1 ){
A[k] = L[i++];
k++;
}
while( j < n2 ){
A[k] = R[j++];
k++;
}
}
void quickSort( int* arrayToSort,int lhv, int rhv){
if(lhv < rhv){
int q = partition(arrayToSort,lhv,rhv);
quickSort(arrayToSort, lhv, q - 1);
quickSort(arrayToSort, q+1, rhv);
}
}
int partition( int* A, int p, int r){
int x = A[r];
int i = p - 1;
int j;
int temp;
for(j = p; j < r; j++){
if(A[j] <= x ){
i++;
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
temp = A[i+1];
A[i+1] = A[r];
A[r] = temp;
return i+1;
}
void bubbleSort( int* arrayToSort, int size ){
BOOL swapped = TRUE;
int j = 0;
int i = 0;
int temp;
while( swapped ){
swapped = FALSE;
j++;
for( i = 0; i < size - j; i++ ){
if(arrayToSort[i] > arrayToSort[i+1]){
temp = arrayToSort[i];
arrayToSort[i] = arrayToSort[i+1];
arrayToSort[i+1] = temp;
swapped = TRUE;
}
}
}
}
void heapSort( int* arrayToSort, int size ){
int end;
int temp;
heapify( arrayToSort, size );
end = size - 1;
while( end > 0 ){
temp = arrayToSort[0];
arrayToSort[0] = arrayToSort[end];
arrayToSort[end] = temp;
end--;
siftDown(arrayToSort, 0, end);
}
}
void heapify( int* arrayToSort, int size ){
int start = floor((size - 2) / 2 );
while( start >= 0 ){
siftDown( arrayToSort, start, size - 1 );
start --;
}
}
void siftDown( int* arrayToSort, int start, int end ){
int root = start;
int child;
int swap;
int temp;
while( (root * 2 + 1) <= end ){
child = root * 2 + 1;
swap = root;
if( arrayToSort[swap] < arrayToSort[child] ){
swap = child;
}
if( (child+1) <= end && arrayToSort[swap] < arrayToSort[child+1] ){
swap = child + 1;
}
if( swap == root ){
break;
}else{
temp = arrayToSort[root];
arrayToSort[root] = arrayToSort[swap];
arrayToSort[swap] = temp;
root = swap;
}
}
}
//------------------------------------------------------------
void printArray( int* array, int size, int mod){
int i = 0;
printf("Array: ");
for(i = 0; i < size; i++){
if(i%mod == 0){ printf("\n"); }
printf("%i ",array[i]);
}
printf("\n\n");
}
void randomizeArray( int* array, int size, int mod ){
int i = 0;
for(i = 0; i < size; i++){
array[i] = rand() % mod + 1;
}
}
//------------------------------------------------------------