-
Notifications
You must be signed in to change notification settings - Fork 0
/
integerSorting.h
337 lines (299 loc) · 7.23 KB
/
integerSorting.h
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
// function prototypes
int* allocateSpace(int); // to create space for an array
int* allocateSpaceInit(int); // to create space for an array and initialize it with 0's
int* populateArray(int *,int); // to populate an array
void displayArray(int *,int); // to display an array
int* bubbleSort(int *,int); // to perform Bubble Sort
int* bubbleSortAdaptive(int *,int); // to perform Bubble Sort (an efficient version in the best case)
int* selectionSort(int *,int); // to perform Selection Sort
int* insertionSort(int *,int); // to perform Insertion Sort
int* shellSort(int *,int); // to perform Shell Sort
int* sortInterval(int *,int,int,int); // to perform Insertion Sort on the chosen interval (refer Shell sort)
int* mergeSort(int *,int,int); // to perform Merge Sort (divide)
int* merge(int *,int,int,int); // to merge the subsequent divisions (conquer)
int* mergeEfficient(int *,int,int,int); // to merge the subsequent divisions (conquer) (Efficient version)
int* quickSort(int *,int,int); // to perform Quick Sort (conquer)
int* partitionHoare(int *,int,int,int *); // to perform partion using Hoare partition
int* partitionLomuto(int *,int,int,int *); // to perform partion using Lomuto partition
int findMax(int *,int); // to find the maximum from an array
int* countingSort(int *,int); // to perform Counting Sort
// function definitions
int* allocateSpace(int n)
// to create space for an array
{
return (int *)malloc(n*sizeof(int));
}
int* allocateSpaceInit(int n)
// to create space for an array and initialize it with 0's
{
return (int *)calloc(n,sizeof(int));
}
int* populateArray(int *a,int n)
// to populate an array
{
int i;
for(i = 0; i < n; ++i)
{
printf("\nElement %d : ",i+1);
scanf("%d",&a[i]);
}
return a;
}
void displayArray(int *a,int n)
// to display an array
{
int i;
for( i = 0; i < n; ++i)
if(i==0)
printf("\n[%d",a[i]);
else if(i == n-1)
printf("%d]\n\n",a[i]);
else
printf(" %d ",a[i]);
}
int *bubbleSort(int *a,int n)
// to perform Bubble Sort
{
int i, j, temp;
for(i = 0 ; i < n-1 ; ++i)
for(j = 0; j < n-i-1 ; ++j)
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
return a;
}
int* bubbleSortAdaptive(int *a,int n)
// to perform Bubble Sort (an efficient version in the best case)
{
int i, j, temp, swapped;
swapped = TRUE;
for(i = 0 ; i < n-1 && swapped; ++i)
{
swapped = FALSE;
for(j = 0; j < n-i-1 ; ++j)
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
swapped = TRUE;
}
}
return a;
}
int* selectionSort(int *a,int n)
// to perform Selection Sort
{
int i, j, temp, minIndex;
for(i = 0; i < n-1; ++i)
{
minIndex = i;
for(j = i+1; j < n; ++j)
if(a[j] < a[minIndex])
minIndex = j;
temp = a[minIndex];
a[minIndex] = a[i];
a[i] = temp;
}
return a;
}
int* insertionSort(int *a,int n)
// to perform Insertion Sort
{
int i, j, temp;
for(i = 1; i < n; ++i)
{
temp = a[i];
for(j = i-1; j>=0 && a[j]>temp; --j)
a[j+1] = a[j];
a[j+1] = temp;
}
return a;
}
int* shellSort(int *a,int n)
// to perform Shell Sort
{
int increment, start;
increment = n;
do
{
increment = increment/3; // change according to any gaping sequence
for(start = 0; start < increment; ++start)
a = sortInterval(a,n,start,increment);
//printf("\nincrement=%d\n",increment);
}
while(increment > 1);
return a;
}
int* sortInterval(int *a,int n,int start,int increment)
// to perform Insertion Sort on the chosen interval (refer Shell sort)
{
int i, j, temp;
for(i = start + increment; i < n; i = i + increment)
{
temp = a[i];
for(j = i - increment; j>=start && a[j]>temp; j = j - increment)
a[j+increment] = a[j];
a[j+increment] = temp;
}
return a;
}
int* mergeSort(int *a,int low,int high)
// to perform Merge Sort (divide)
{
int mid;
if(low < high)
{
mid = low + (high-low)/2;
// DIVIDE
a = mergeSort(a,low,mid);
a = mergeSort(a,mid+1,high);
// CONQUER
a = mergeEfficient(a,low,mid,high); // the merge() function can also be invoked in this step
// mergeEfficient() is used because it doesn't copy all elements to the auxiliary array
}
return a;
}
int *merge(int *a,int low,int mid,int high)
// to merge the subsequent divisions (conquer)
{
int *b, i, j, k;
b = allocateSpace(high-low+mid); // auxiliary array
for(i = low; i<= high; ++i)
b[i] = a[i]; // copy all elements
i = low, j = mid + 1, k = low;
while(i<=mid && j<=high)
{
if(b[i] <= b[j])
a[k++] = b[i++];
else
a[k++] = b[j++];
}
while(i<=mid)
a[k++] = b[i++];
return a;
}
int *mergeEfficient(int *a,int low,int mid,int high)
/* to merge the subsequent divisions (conquer)
Efficient version: The auxiliary array 'b' stores only the first half elements */
{
int *b, i, j, k;
b = allocateSpace(high-low+mid); // auxiliary array
for(i = 0, j = low; j<= mid; ++i, ++j)
b[i] = a[j]; // copy all elements
i = 0, k = low;
while(k<j && j<=high)
{
if(b[i] <= a[j])
a[k++] = b[i++];
else
a[k++] = a[j++];
}
while(k<j)
a[k++] = b[i++];
return a;
}
int* quickSort(int *a,int low,int high)
// to perform Quick Sort (conquer)
{
int pivot;
if(low<high)
{
a = partitionLomuto(a,low,high,&pivot);
/*
partitionLomuto() uses one index and use it to traverse the array
partitionHoare() uses two indices and uses to to traverse the array from either directions
*/
// pivot is placed at its original position
a = quickSort(a,low,pivot-1);
a = quickSort(a,pivot+1,high);
}
return a;
}
int* partitionHoare(int *a,int low,int high,int *fixed)
// to perform partion using Hoare partition
{
int i,j,pivot,temp;
i = low;
j = high;
pivot = a[low];
while(i<j)
{
while(a[i]<=pivot && i<=high)
++i;
while(a[j]>pivot)
--j;
if(i<j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
temp = a[j];
a[j] = a[low];
a[low] = temp;
*fixed = j;
return a;
}
int* partitionLomuto(int *a,int low,int high,int *fixed)
// to perform partion using Lomuto partition
{
int i,j,temp,pivot;
i = low - 1;
pivot = a[high];
for(j = low; j < high; ++j)
{
if(a[j] <= pivot)
{
++i;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
temp = a[i+1];
a [i+1] = a[high];
a[high] = temp;
*fixed = i+1;
return a;
}
int findMax(int *a,int n)
// to find the maximum from an array
{
int i,max;
max = a[0];
for(i = 1 ; i < n; ++i)
if( a[i] > max)
max = a[i];
return max;
}
int* countingSort(int *A,int n)
// to perform Counting Sort
{
int *B, *C, i, j, k;
k = findMax(A,n);
B = allocateSpaceInit(n);
C = allocateSpaceInit(k+1);
for(j = 0; j < n; ++j)
C[A[j]] = C[A[j]] + 1;
printf("\nThe frequency array: \n");
displayArray(C,k+1);
for(i = 1; i <= k; ++i)
C[i] = C[i] + C[i-1];
printf("\nThe cumulative frequency array: \n");
displayArray(C,k+1);
for(j = n-1; j>=0; --j)
{
B[C[A[j]]-1] = A[j];
C[A[j]] = C[A[j]] - 1;
}
return B;
}