-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSortingAlgorithms.cpp
More file actions
272 lines (228 loc) · 6.59 KB
/
Copy pathSortingAlgorithms.cpp
File metadata and controls
272 lines (228 loc) · 6.59 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
#include "SortingAlgorithms.h"
#include <time.h>
#include <limits.h>
#define NUMBERS_PER_LINE_TO_PRINT 5
// Returns the count
void SortingAlgorithms::SetCount(int count)
{
this->count = count;
}
// Prints the list:
void SortingAlgorithms::PrintList()
{
int numbersPrintedSoFar = 0;
if (arr == nullptr)
return;
cout << "Displaying " << NUMBERS_PER_LINE_TO_PRINT <<" elements per line\n";
for (int i = 0; i < count; i++)
{
cout << " " << arr[i] << "\t|";
numbersPrintedSoFar++;
if (numbersPrintedSoFar == NUMBERS_PER_LINE_TO_PRINT)
{
cout << "\n";
numbersPrintedSoFar = 0;
}
}
}
// Default Constructor:
SortingAlgorithms::SortingAlgorithms()
{
count = 0;
arr = nullptr;
}
// Destructor
SortingAlgorithms::~SortingAlgorithms()
{
if (this->arr != nullptr)
delete[] arr;
}
// Clears the array and then randomizes it:
void SortingAlgorithms::Randomize()
{
if (arr == nullptr)
arr = new int[count];
else
{
delete[] arr;
arr = new int[count];
}
srand(time(NULL));
cout << "Seeding random number generator and creating new array" << endl;
for (int i = 0; i < count; i++)
arr[i] = rand() % 1000 + 1;
}
void SortingAlgorithms::Swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
bool SortingAlgorithms::isSorted()
{
for (int i = 0; i < count; i++)
if (arr[i] > arr[i + 1])
return false;
return true;
}
int SortingAlgorithms::FindIndexMin(int *array, int size)
{
int indexMin = 0;
int minSoFar = INT_MAX;
for (int i = 0; i < size; i++)
if (array[i] < minSoFar)
{
indexMin = i;
minSoFar = array[i];
}
return indexMin;
}
// |===========================================================================|
// | Bubble Sort |
// |===========================================================================|
void SortingAlgorithms::BubbleSort()
{
bool swappedSomething = false;
for (int i = 0; i < count; i++)
{
for (int j = 0; j < count - i; j++)
{
if (arr[j] > arr[j + 1])
{
swap(arr[j], arr[j + 1]);
swappedSomething = true;
}
}
if (!swappedSomething)
break;
else
swappedSomething = false;
}
}
// |===========================================================================|
// | Selection Sort |
// |===========================================================================|
void SortingAlgorithms::SelectionSort()
{
int minIndex = 0;
for (int i = 0; i < count; i++)
{
minIndex = i;
for (int j = i + 1; j < count; j++)
if (arr[j] < arr[minIndex])
minIndex = j;
Swap(arr + i, arr + minIndex);
}
}
// |===========================================================================|
// | Insertion Sort |
// |===========================================================================|
void SortingAlgorithms::InsertionSort()
{
int x, y, z;
for (z = 1; z < count; z++)
{
y = arr[z];
for (x = z; x > 0 && arr[x] < arr[x-1]; x--)
{
Swap(arr + x, arr + x - 1);
}
}
}
// |===========================================================================|
// | Merge Sort |
// |===========================================================================|
void SortingAlgorithms::MergeSortHelper(int *arr, int lo, int hi)
{
int mid = lo + (hi - lo) / 2, i = lo, j = mid + 1, k = 0;
int *aux;
if (lo >= hi)
return;
// Recursive calls
MergeSortHelper(arr, lo, mid);
MergeSortHelper(arr, mid + 1, hi);
aux = new int[hi - lo + 1];
while (i <= mid || j <= hi)
{
if (i > mid)
aux[k++] = arr[j++];
else if (j > hi)
aux[k++] = arr[i++];
else if (arr[i] < arr[j])
aux[k++] = arr[i++];
else
aux[k++] = arr[j++];
}
for (i = lo; i <= hi; i++)
arr[i] = aux[i - lo];
delete[] aux;
}
void SortingAlgorithms::MergeSort()
{
// Recursive call to help with merge sort:
MergeSortHelper(arr, 0, count - 1);
}
// |===========================================================================|
// | Quick Sort |
// |===========================================================================|
int SortingAlgorithms::PartitionArray(int *arr, int lo, int hi)
{
int pivot = arr[hi];
int i = (lo - 1);
for (int j = lo; j <= hi - 1; j++)
{
if (arr[j] <= pivot)
{
i++;
Swap(&arr[i], &arr[j]);
}
}
Swap(&arr[i + 1], &arr[hi]);
return (i + 1);
}
void SortingAlgorithms::QuickSortHelper(int *arr, int lo, int hi)
{
if (lo < hi)
{
int mid = PartitionArray(arr, lo, hi);
QuickSortHelper(arr, lo, mid - 1);
QuickSortHelper(arr, mid + 1, hi);
}
}
void SortingAlgorithms::QuickSort()
{
QuickSortHelper(arr, 0, count - 1);
}
// |===========================================================================|
// | SHell Sort |
// |===========================================================================|
int SortingAlgorithms::ShellSortHelper(int *arr, int n)
{
// Start with a big gap, then reduce the gap
for (int gap = n/2; gap > 0; gap /= 2)
{
// Do a gapped insertion sort for this gap size.
// The first gap elements a[0..gap-1] are already in gapped order
// keep adding one more element until the entire array is
// gap sorted
for (int i = gap; i < n; i += 1)
{
// add a[i] to the elements that have been gap sorted
// save a[i] in temp and make a hole at position i
int temp = arr[i];
// shift earlier gap-sorted elements up until the correct
// location for a[i] is found
int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
arr[j] = arr[j - gap];
// put temp (the original a[i]) in its correct location
arr[j] = temp;
}
}
return 0;
}
/* function to sort arr using shellSort */
void SortingAlgorithms::ShellSort()
{
ShellSortHelper(arr, count);
}