-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSA21.c
More file actions
306 lines (259 loc) · 10.8 KB
/
Copy pathDSA21.c
File metadata and controls
306 lines (259 loc) · 10.8 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
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
/*
This code base own and maintained by Tanmoy Samnata using standardization C23 (ISO/IEC 9899:2024)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// UTILITY FUNCTIONS
void swapElements(int *firstElementPointer, int *secondElementPointer)
{
int temporaryStorage = *firstElementPointer;
*firstElementPointer = *secondElementPointer;
*secondElementPointer = temporaryStorage;
}
void printArrayElements(const char *sortName, int targetArray[], int totalElements)
{
printf("[%s]:\n", sortName);
for (int currentIndex = 0; currentIndex < totalElements; currentIndex++)
{
printf("%d ", targetArray[currentIndex]);
}
printf("\n\n");
}
// BUBBLE SORT
void executeBubbleSort(int targetArray[], int arrayLength)
{
for (int outerPass = 0; outerPass < arrayLength - 1; outerPass++)
{
for (int innerCompareIndex = 0; innerCompareIndex < arrayLength - outerPass - 1; innerCompareIndex++)
{
if (targetArray[innerCompareIndex] > targetArray[innerCompareIndex + 1])
{
swapElements(&targetArray[innerCompareIndex], &targetArray[innerCompareIndex + 1]);
}
}
}
}
// SELECTION SORT
void executeSelectionSort(int targetArray[], int arrayLength)
{
for (int currentStepIndex = 0; currentStepIndex < arrayLength - 1; currentStepIndex++)
{
int minimumValueIndex = currentStepIndex;
for (int searchIndex = currentStepIndex + 1; searchIndex < arrayLength; searchIndex++)
{
if (targetArray[searchIndex] < targetArray[minimumValueIndex])
{
minimumValueIndex = searchIndex;
}
}
swapElements(&targetArray[minimumValueIndex], &targetArray[currentStepIndex]);
}
}
// INSERTION SORT
void executeInsertionSort(int targetArray[], int arrayLength)
{
for (int stepIndex = 1; stepIndex < arrayLength; stepIndex++)
{
int keyElementToInsert = targetArray[stepIndex];
int previousElementIndex = stepIndex - 1;
while (previousElementIndex >= 0 && targetArray[previousElementIndex] > keyElementToInsert)
{
targetArray[previousElementIndex + 1] = targetArray[previousElementIndex];
previousElementIndex--;
}
targetArray[previousElementIndex + 1] = keyElementToInsert;
}
}
// SHELL SORT
void executeShellSort(int targetArray[], int arrayLength)
{
for (int currentGapSize = arrayLength / 2; currentGapSize > 0; currentGapSize /= 2)
{
for (int currentIndex = currentGapSize; currentIndex < arrayLength; currentIndex++)
{
int temporaryElement = targetArray[currentIndex];
int shiftIndex;
for (shiftIndex = currentIndex;
shiftIndex >= currentGapSize && targetArray[shiftIndex - currentGapSize] > temporaryElement;
shiftIndex -= currentGapSize)
{
targetArray[shiftIndex] = targetArray[shiftIndex - currentGapSize];
}
targetArray[shiftIndex] = temporaryElement;
}
}
}
// MERGE SORT
void mergeSubArrays(int targetArray[], int leftBoundaryIndex, int middlePointIndex, int rightBoundaryIndex)
{
int leftPartitionSize = middlePointIndex - leftBoundaryIndex + 1;
int rightPartitionSize = rightBoundaryIndex - middlePointIndex;
int *leftTemporaryArray = malloc(leftPartitionSize * sizeof(int));
int *rightTemporaryArray = malloc(rightPartitionSize * sizeof(int));
for (int copyLeftIndex = 0; copyLeftIndex < leftPartitionSize; copyLeftIndex++)
{
leftTemporaryArray[copyLeftIndex] = targetArray[leftBoundaryIndex + copyLeftIndex];
}
for (int copyRightIndex = 0; copyRightIndex < rightPartitionSize; copyRightIndex++)
{
rightTemporaryArray[copyRightIndex] = targetArray[middlePointIndex + 1 + copyRightIndex];
}
int trackingLeftIndex = 0;
int trackingRightIndex = 0;
int mergedArrayIndex = leftBoundaryIndex;
while (trackingLeftIndex < leftPartitionSize && trackingRightIndex < rightPartitionSize)
{
if (leftTemporaryArray[trackingLeftIndex] <= rightTemporaryArray[trackingRightIndex])
{
targetArray[mergedArrayIndex] = leftTemporaryArray[trackingLeftIndex];
trackingLeftIndex++;
}
else
{
targetArray[mergedArrayIndex] = rightTemporaryArray[trackingRightIndex];
trackingRightIndex++;
}
mergedArrayIndex++;
}
while (trackingLeftIndex < leftPartitionSize)
{
targetArray[mergedArrayIndex] = leftTemporaryArray[trackingLeftIndex];
trackingLeftIndex++;
mergedArrayIndex++;
}
while (trackingRightIndex < rightPartitionSize)
{
targetArray[mergedArrayIndex] = rightTemporaryArray[trackingRightIndex];
trackingRightIndex++;
mergedArrayIndex++;
}
free(leftTemporaryArray);
free(rightTemporaryArray);
}
void executeMergeSort(int targetArray[], int leftBoundaryIndex, int rightBoundaryIndex)
{
if (leftBoundaryIndex < rightBoundaryIndex)
{
int middlePointIndex = leftBoundaryIndex + (rightBoundaryIndex - leftBoundaryIndex) / 2;
executeMergeSort(targetArray, leftBoundaryIndex, middlePointIndex);
executeMergeSort(targetArray, middlePointIndex + 1, rightBoundaryIndex);
mergeSubArrays(targetArray, leftBoundaryIndex, middlePointIndex, rightBoundaryIndex);
}
}
// 6. HEAP SORT
void organizeHeapShape(int targetArray[], int arrayLength, int rootNodeIndex)
{
int largestValueIndex = rootNodeIndex;
int leftChildNodeIndex = 2 * rootNodeIndex + 1;
int rightChildNodeIndex = 2 * rootNodeIndex + 2;
if (leftChildNodeIndex < arrayLength && targetArray[leftChildNodeIndex] > targetArray[largestValueIndex])
{
largestValueIndex = leftChildNodeIndex;
}
if (rightChildNodeIndex < arrayLength && targetArray[rightChildNodeIndex] > targetArray[largestValueIndex])
{
largestValueIndex = rightChildNodeIndex;
}
if (largestValueIndex != rootNodeIndex)
{
swapElements(&targetArray[rootNodeIndex], &targetArray[largestValueIndex]);
organizeHeapShape(targetArray, arrayLength, largestValueIndex);
}
}
void executeHeapSort(int targetArray[], int arrayLength)
{
for (int initialStructureNode = arrayLength / 2 - 1; initialStructureNode >= 0; initialStructureNode--)
{
organizeHeapShape(targetArray, arrayLength, initialStructureNode);
}
for (int extractionNodeIndex = arrayLength - 1; extractionNodeIndex > 0; extractionNodeIndex--)
{
swapElements(&targetArray[0], &targetArray[extractionNodeIndex]);
organizeHeapShape(targetArray, extractionNodeIndex, 0);
}
}
// 7. RADIX SORT
int findMaximumValue(int targetArray[], int arrayLength)
{
int absoluteMaximum = targetArray[0];
for (int searchIndex = 1; searchIndex < arrayLength; searchIndex++)
{
if (targetArray[searchIndex] > absoluteMaximum)
{
absoluteMaximum = targetArray[searchIndex];
}
}
return absoluteMaximum;
}
void countingSortForRadix(int targetArray[], int arrayLength, int currentPlaceValue)
{
int *temporaryOutputArray = malloc(arrayLength * sizeof(int));
int digitFrequencyCount[10] = {0};
for (int frequencyIndex = 0; frequencyIndex < arrayLength; frequencyIndex++)
{
int isolatedDigit = (targetArray[frequencyIndex] / currentPlaceValue) % 10;
digitFrequencyCount[isolatedDigit]++;
}
for (int cumulativeIndex = 1; cumulativeIndex < 10; cumulativeIndex++)
{
digitFrequencyCount[cumulativeIndex] += digitFrequencyCount[cumulativeIndex - 1];
}
for (int reversePlacementIndex = arrayLength - 1; reversePlacementIndex >= 0; reversePlacementIndex--)
{
int isolatedDigit = (targetArray[reversePlacementIndex] / currentPlaceValue) % 10;
temporaryOutputArray[digitFrequencyCount[isolatedDigit] - 1] = targetArray[reversePlacementIndex];
digitFrequencyCount[isolatedDigit]--;
}
for (int finalCopyIndex = 0; finalCopyIndex < arrayLength; finalCopyIndex++)
{
targetArray[finalCopyIndex] = temporaryOutputArray[finalCopyIndex];
}
free(temporaryOutputArray);
}
void executeRadixSort(int targetArray[], int arrayLength)
{
int maximumElementInArray = findMaximumValue(targetArray, arrayLength);
for (int processingPlaceValue = 1; maximumElementInArray / processingPlaceValue > 0; processingPlaceValue *= 10)
{
countingSortForRadix(targetArray, arrayLength, processingPlaceValue);
}
}
int main(void)
{
int originalBaseDataset[] = {88, 14, 39, 72, 55, 23, 91, 19, 66, 8, 41, 105};
int totalElementsInDataset = sizeof(originalBaseDataset) / sizeof(originalBaseDataset[0]);
int *activeWorkingArray = malloc(totalElementsInDataset * sizeof(int));
printf("=== SORTING ALGORITHMS DISPLAY ===\n\n");
printArrayElements("Original Unsorted Dataset", originalBaseDataset, totalElementsInDataset);
/* Bubble Sort */
memcpy(activeWorkingArray, originalBaseDataset, totalElementsInDataset * sizeof(int));
executeBubbleSort(activeWorkingArray, totalElementsInDataset);
printArrayElements("Bubble Sort Result", activeWorkingArray, totalElementsInDataset);
/* Selection Sort */
memcpy(activeWorkingArray, originalBaseDataset, totalElementsInDataset * sizeof(int));
executeSelectionSort(activeWorkingArray, totalElementsInDataset);
printArrayElements("Selection Sort Result", activeWorkingArray, totalElementsInDataset);
/* Insertion Sort */
memcpy(activeWorkingArray, originalBaseDataset, totalElementsInDataset * sizeof(int));
executeInsertionSort(activeWorkingArray, totalElementsInDataset);
printArrayElements("Insertion Sort Result", activeWorkingArray, totalElementsInDataset);
/* Shell Sort */
memcpy(activeWorkingArray, originalBaseDataset, totalElementsInDataset * sizeof(int));
executeShellSort(activeWorkingArray, totalElementsInDataset);
printArrayElements("Shell Sort Result", activeWorkingArray, totalElementsInDataset);
/* Merge Sort */
memcpy(activeWorkingArray, originalBaseDataset, totalElementsInDataset * sizeof(int));
executeMergeSort(activeWorkingArray, 0, totalElementsInDataset - 1);
printArrayElements("Merge Sort Result", activeWorkingArray, totalElementsInDataset);
/* Heap Sort */
memcpy(activeWorkingArray, originalBaseDataset, totalElementsInDataset * sizeof(int));
executeHeapSort(activeWorkingArray, totalElementsInDataset);
printArrayElements("Heap Sort Result", activeWorkingArray, totalElementsInDataset);
/* Radix Sort */
memcpy(activeWorkingArray, originalBaseDataset, totalElementsInDataset * sizeof(int));
executeRadixSort(activeWorkingArray, totalElementsInDataset);
printArrayElements("Radix Sort Result", activeWorkingArray, totalElementsInDataset);
free(activeWorkingArray);
return 0;
}