-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting_algorithms.C
216 lines (183 loc) · 5.93 KB
/
sorting_algorithms.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
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "functions.h"
// Shuffle array for bars to be in random order
void shuffleArray(int arr[], int n) {
for (int i = n - 1; i > 0; i--) {
int j = rand() % (i + 1);
Swap(&arr[i], &arr[j]);
}
}
int Sort(SDL_Renderer *renderer) {
// Initialize array with sequential values
int arr[NUM_BARS];
for (int i = 0; i < NUM_BARS; i++) {
arr[i] = ((i + 1) * WINDOW_HEIGHT) / NUM_BARS;
}
// Shuffle the array to randomize the order
srand(time(NULL));
shuffleArray(arr, NUM_BARS);
// Draw initial bars
DrawBars(renderer, arr, BLUE, -1, PAUSE);
// Sort the array and visualize it
int sorted = 0;
if (strcmp(ALGORITHM, "BubbleSort") == 0) {
sorted = BubbleSort(arr, NUM_BARS, renderer);
}
else if (strcmp(ALGORITHM, "SelectionSort") == 0) {
sorted = SelectionSort(arr, NUM_BARS, renderer);
}
else if (strcmp(ALGORITHM, "InsertionSort") == 0) {
sorted = InsertionSort(arr, NUM_BARS, renderer);
}
else if (strcmp(ALGORITHM, "QuickSort") == 0) {
sorted = QuickSort(arr, NUM_BARS, renderer);
}
else if (strcmp(ALGORITHM, "MergeSort") == 0) {
sorted = MergeSort(arr, NUM_BARS, renderer);
}
else if (strcmp(ALGORITHM, "BucketSort") == 0) {
sorted = BucketSort(arr, NUM_BARS, renderer);
}
else if (strcmp(ALGORITHM, "ShellSort") == 0) {
sorted = ShellSort(arr, NUM_BARS, renderer);
}
else if (strcmp(ALGORITHM, "HeapSort") == 0) {
sorted = HeapSort(arr, NUM_BARS, renderer);
}
else {
sorted = 0;
}
// Draw the final sorted bars
if (sorted == 1) {
DrawBars(renderer, arr, GREEN, -1, PAUSE);
return 1;
}
else {
return 0;
}
}
int BubbleSort(int arr[], int n, SDL_Renderer *renderer) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
// Draw the bars and highlight the swapped bar
DrawBars(renderer, arr, BLUE, j + 1, PAUSE);
}
}
}
return 1;
}
int SelectionSort(int arr[], int n, SDL_Renderer *renderer) {
for (int i = 0; i < n - 1; i++) {
// Assume the minimum is the first element
int minIndex = i;
// Find the index of the minimum element in the remaining unsorted part
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the first unsorted element
if (minIndex != i) {
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
// Draw the bars and highlight the swapped bars
DrawBars(renderer, arr, BLUE, minIndex, PAUSE);
DrawBars(renderer, arr, BLUE, i, PAUSE);
}
}
return 1;
}
int InsertionSort(int arr[], int n, SDL_Renderer *renderer) {
int i, j, key;
// Iterate over the array from the second element to the end
for (i = 1; i < n; i++) {
key = arr[i];
// Move elements of the sorted portion that are greater than key to one position ahead
j = i - 1;
while (j >= 0 && arr[j] > key) {
DrawBars(renderer, arr, BLUE, j, PAUSE);
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
return 1;
}
int QuickSort(int arr[], int n, SDL_Renderer *renderer) {
QuickSortRecursive(arr, 0, n - 1, renderer);
return 1;
}
int MergeSort(int arr[], int n, SDL_Renderer *renderer) {
MergeSortRecursive(arr, 0, n - 1, renderer);
return 1;
}
int BucketSort(int arr[], int n, SDL_Renderer *renderer) {
int max_value = WINDOW_HEIGHT;
int num_buckets = NUM_BARS;
int buckets[num_buckets][n];
int bucket_sizes[num_buckets];
// Initialize all bucket sizes to 0
for (int i = 0; i < num_buckets; i++) {
bucket_sizes[i] = 0;
}
// Distribute array elements into buckets
for (int i = 0; i < n; i++) {
int bucket_index = (arr[i] * num_buckets) / (max_value + 1);
buckets[bucket_index][bucket_sizes[bucket_index]++] = arr[i];
DrawBars(renderer, arr, BLUE, i, PAUSE);
}
// Sort each bucket and merge back into the original array
int index = 0;
for (int i = 0; i < num_buckets; i++) {
InsertionSort(buckets[i], bucket_sizes[i], renderer);
for (int j = 0; j < bucket_sizes[i]; j++) {
arr[index++] = buckets[i][j];
DrawBars(renderer, arr, BLUE, i, PAUSE);
}
}
return 1;
}
int ShellSort(int arr[], int n, SDL_Renderer *renderer) {
for (int gap = n / 2; gap > 0; gap /= 2) {
// Perform a gapped insertion sort
for (int i = gap; i < n; i++) {
int temp = arr[i];
int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
arr[j] = arr[j - gap];
// Visualize the swap
DrawBars(renderer, arr, BLUE, j, PAUSE);
}
arr[j] = temp;
// Visualize the insertion
DrawBars(renderer, arr, BLUE, j, PAUSE);
}
}
return 1;
}
int HeapSort(int arr[], int n, SDL_Renderer *renderer) {
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
Heapify(arr, n, i, renderer);
// One by one extract an element from heap
for (int i = n - 1; i > 0; i--) {
// Move current root to end
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
// Visualize the swap
DrawBars(renderer, arr, BLUE, i, PAUSE);
// Call max heapify on the reduced heap
Heapify(arr, i, 0, renderer);
}
return 1;
}