-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
348 lines (305 loc) · 9.67 KB
/
Copy pathmain.cpp
File metadata and controls
348 lines (305 loc) · 9.67 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
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
338
339
340
341
342
343
344
345
346
347
348
/******************************
This program will implement six sorting algorithms on an array of randomly generated number
******************************/
#include <iostream>
#include <cstdlib>
using namespace std;
//prototypes
void initArray(int A[], int len);
void selectionSort(int A[], int len);
void bubbleSort(int A[], int len);
void insertionSort(int A [], int len);
void quickSort(int A[],int len);
void quickSortFunc(int A[], int low, int high);
void mergeSort(int A[],int len);
void heapSort(int A[], int len);
void printArray(int A[], int len);
void swap(int &x, int &y);
void mergeSort(int A[], int len);
void mergeSortFunc(int A[], int l, int r);
void merge(int A[], int l, int m, int r);
int partition(int A[], int low, int high);
void heapify(int A[], int n, int i);
/******************************
Main function will create an array of integers and call all six sorting algorithms
******************************/
int main()
{
int size = 10;//set the size of the array
int array[size];//initialize the array
initArray(array,size);//initialize the array before every sorting algorithm
cout<<"\n\tOriginal Array:"<<endl;
printArray(array,size);
selectionSort(array,size);
cout<<"\tSelection Sort:"<<endl;
printArray(array,size);
initArray(array,size);
cout<<"\n\tOriginal Array:"<<endl;
printArray(array,size);
bubbleSort(array,size);
cout<<"\tBubble Sort:"<<endl;
printArray(array,size);
initArray(array,size);
cout<<"\n\tOriginal Array:"<<endl;
printArray(array,size);
insertionSort(array,size);
cout<<"\tInsertion Sort:"<<endl;
printArray(array,size);
initArray(array,size);
cout<<"\n\tOriginal Array:"<<endl;
printArray(array,size);
quickSort(array,size);
cout<<"\tQuick Sort:"<<endl;
printArray(array,size);
initArray(array,size);
cout<<"\n\tOriginal Array:"<<endl;
printArray(array,size);
mergeSort(array,size);
cout<<"\tMerge Sort:"<<endl;
printArray(array,size);
initArray(array,size);
cout<<"\n\tOriginal Array:"<<endl;
printArray(array,size);
heapSort(array,size);
cout<<"\tHeap Sort:"<<endl;
printArray(array,size);
}
/******************************
This function will initialize the array storing random numbers into it
Parameters: int A[] - the array , int len - the size of the array
Return: void
******************************/
void initArray(int A[], int len)
{
srand(time(0));//rand seed time
int s = 0;//count
for(s;s < len;s++)//create array sized len of random numbers
{
int num = rand()%100+1;
A[s] = num;
}
}
/******************************
This function will implement the selection sort algorithm
Parameters: int A[] - the array , int len - the size of the array
Return: void
******************************/
void selectionSort(int A[], int len)
{
int f,n,min;
for(f=0;f<len-1;f++)//loop through the array
{
min = f;//set first value to smallest value
for(n = f+1; n < len; n++)//loop through array length - 1
if(A[n] < A[min])//if value is smaller than the one in min then that value beecomes min
min = n;
swap(A[min], A[f]);//swap the two values
}
}
/******************************
This function will implement the bubble sort algorithm
Parameters: int A[] - the array , int len - the size of the array
Return: void
******************************/
void bubbleSort(int A[], int len)
{
int f,n;
for(f = 0; f < len-1; f++)//loop through the array
for(n = 0;n<len-1;n++)//loop through array length - 1
if(A[n] > A[n+1])//if value is greater than the one we are currently at then "bubble it to the top"
swap(A[n], A[n+1]);//swap those two values
}
/******************************
This function will implement the insertion sort algorithm
Parameters: int A[] - the array , int len - the size of the array
Return: void
******************************/
void insertionSort(int A [], int len)
{
int f,n,key;
for(f = 1;f<len;f++)//loop through array
{
key = A[f];//current num is our key
n = f - 1;
while(n >= 0 && A[n] > key)//while value is greater than 0 and greater than our key
{
A[n+1] = A[n];
n = n - 1;
}
A[n + 1] = key;//set key to next value
}
}
/******************************
This function will implement the quick sort algorithm
Parameters: int A[] - the array , int len - the size of the array
Return: void
******************************/
void quickSort(int A[], int len)
{//Inorder to call quickSort with only two parameters, we have to set the initial low and high
quickSortFunc(A,0,len-1);//call quicksort
}
/******************************
This function will implement quickSort recursivley
Parameters: int A[] - main array, int low - low limit, int high - high limit
Return: void
******************************/
void quickSortFunc(int A[], int low, int high)
{
if(low >= high)//base case
return;
else
{
int temp = partition(A,low,high);//get pivot by calling partition
quickSortFunc(A,low,high-1);//quicksort left subarray
quickSortFunc(A,low+1,high);//quicksort right subarray
}
}
/******************************
This function will implement the merge sort algorithm
Parameters: int A[] - the array , int len - the size of the array
Return: void
******************************/
void mergeSort(int A[], int len)
{//To call mergeSort with 2 parameters, set the initial limits
mergeSortFunc(A,0,len-1);
}
/******************************
This function will implement the mergeSort recursivley
Parameters: Int A[] - main array, int l - left index, int r - right index
Return: void
******************************/
void mergeSortFunc(int A[], int l, int r)
{
if(l < r)//while index's havnt crossed
{
int m = l + (r - l)/2;//middle point
mergeSortFunc(A,l,m);//mergesort the left subarray
mergeSortFunc(A,m+1,r);//mergesort the right subarray
merge(A,l,m,r);//merge the two subarrays
}
}
/******************************
This function will implement the heap sort algorithm
Parameters: int A[] - the array , int len - the size of the array
Return: void
******************************/
void heapSort(int A[], int len)
{
//build heap
for(int i = len/2-1; i>=0;i--)
heapify(A,len,i);
//take an element from heap one by one
for(int i = len - 1; i>=0; i--)
{
swap(A[0],A[i]);//swap current value to end
heapify(A, i, 0);//min heap
}
}
/******************************
This function will print the array in order
Parameters: int A[] - the array , int len - the size of the array
Return: void
******************************/
void printArray(int A[], int len)
{
int s = 0;
for(s;s<len;s++)//loop through array
cout<<A[s]<<" ";//print out value
cout<<endl<<endl;
}
/******************************
This function will swap two values within the array
Parameters: int &x - address of first value, int &y - address of second value
Return: void
******************************/
void swap(int &x, int &y)
{
int temp = x;//place first value into temporary int
x = y;//place y into x
y = temp;//make y the valu in temporary holder
}
/******************************
This function will merge the two subarrays created by mergeSort
Parameters: int A[] - main array, int l - left index, int m - middle index, int r - right index
Return: void
******************************/
void merge(int A[], int l, int m, int r)
{
int x,y,z;
int sub1 = m - l + 1;//size of left subarray
int sub2 = r - m;//size of right subarray
int leftA[sub1], rightA[sub2];//create two temporary arrays
//copy the two subarrays into the temporarys
for(x = 0;x < sub1; x++)
leftA[x] = A[l + x];
for(y = 0; y < sub2; y++)
rightA[y] = A[m + 1 + y];
//indexes of the arrays
x = 0;
y = 0;
z = l;
while(x < sub1 && y < sub2)//go through the two subarrays and copy into the maina array
{
if(leftA[x] <= rightA[y])
{
A[z] = leftA[x];
x++;
}
else
{
A[z] = rightA[y];
y++;
}
z++;
}
while(x < sub1)//if theres any values left on left side
{
A[z] = leftA[x];
x++;
z++;
}
while(y < sub2)//if theres any values left on right side
{
A[z] = rightA[y];
y++;
z++;
}
}
/******************************
This function partitions the main array into sorted sub arrays
Parameters: int A[] - main array, int low - low limit, int high - high limit
Return: pivot
******************************/
int partition(int A[], int low, int high)
{
int pivot = A[low];//pivot point
int temp;
int k = temp = low + 1;//smaller element
while(k <= high)//while havnt reached limit of subarray
{
if(A[k] <= pivot)//if current value is less than equal to our pivot then swap them
swap(A[temp++],A[k]);
k++;
}
swap(A[low],A[temp-1]);//swap low with pivot
return temp-1;//return pivot
}
/******************************
This function will create a heap recursivley given an array
Parameters: int A[] - main array, int n - size, int i - current value
******************************/
void heapify(int A[], int n, int i)
{
int largest = i;//largest value is begining
int l = i * 2 + 1;//left side
int r = i * 2 + 2;//right side
if(l<n && A[l] > A[largest])//left child is greater than root
largest = l;
if(r<n && A[r]>A[largest])//right child is greater than root
largest = r;
if(largest != i)//if root isnt largest
{
swap(A[i], A[largest]);//swap it with largest
heapify(A, n, largest);//recursive build heap of subtree
}
}