-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_algorithms.cpp
More file actions
369 lines (273 loc) · 8.2 KB
/
Copy pathsort_algorithms.cpp
File metadata and controls
369 lines (273 loc) · 8.2 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//Author: Brad Bolton
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<iostream>
#include<fstream>
//-----------sorting protos--------------//
void sort(std::vector<int> &);
void bubblesort(std::vector<int> &,int);
void selectionSort(std::vector<int> &);
void insertionSort(std::vector<int> &, int n);
void mergeSort(std::vector<int> &);
void mergeS(std::vector<int> &, std::vector<int>&, int, int);
void merge(std::vector<int> &, std::vector<int>&, int, int, int);
void swap(std::vector<int>& vec, int start, int end);
int partition(std::vector<int>&vec, int start, int end);
void quickSort(std::vector<int>&vec, int start, int end);
void printFile( char,int , float );
void displayvector(std::vector<int> value);
int main()
{
int size = 0;
char method;
std::cout<<"Please enter the size of the array to test: ";
std::cin>>size;
std::vector<int> a(size);
std::generate(a.begin(), a.end(), []{return rand();});
std::clock_t duration;
//---------------------------------------sort funtion--------------------------//
std::cout<<"\n-----------RUNNING SORT ALGORITHM-------------"<<std::endl;
std::cout<<"-----------UNSORTED----------"<<std::endl;
displayvector(a);
duration = clock();
{
sort(a);
}
std::cout<<"-----------SORTED----------"<<std::endl;
duration = clock()-duration;
displayvector(a);
std::cout<<"\n Sort algorithm Time array size clock ticks: "<<(((float)duration)/CLOCKS_PER_SEC)<<std::endl;
method = 'S';
printFile(method,size , duration );
//---------------------------------------bubble sort funtion--------------------------//
std::generate(a.begin(), a.end(), []{return rand();});
std::cout<<"-----------UNSORTED----------"<<std::endl;
displayvector(a);
std::cout<<"\n-----------RUNNING BUBBLE SORT ALGORITHM-------------"<<std::endl;
duration = clock();
{
bubblesort(a,a.size());
}
duration = clock()-duration;
std::cout<<"-----------SORTED----------"<<std::endl;
displayvector(a);
std::cout<<"\n Bubble Sort algorithm Time array size clock ticks: "<<(((float)duration)/CLOCKS_PER_SEC)<<std::endl;
method = 'b';
printFile(method,size , duration );
//---------------------------------------selection sort funtion--------------------------//
std::cout<<"\n-----------RUNNING SELECTION SORT ALGORITHM-------------"<<std::endl;
std::generate(a.begin(), a.end(), []{return rand();});
std::cout<<"-----------UNSORTED----------"<<std::endl;
displayvector(a);
duration = clock();
{
selectionSort(a);
}
duration = clock()-duration;
std::cout<<"-----------SORTED----------"<<std::endl;
displayvector(a);
std::cout<<"\n Selection Sort algorithm Time array clock ticks: "<<(((float)duration)/CLOCKS_PER_SEC)<<std::endl;
method = 's';
printFile(method,size , duration );
//---------------------------------------insertion sort funtion--------------------------//
std::cout<<"\n-----------insertion SELECTION SORT ALGORITHM-------------"<<std::endl;
std::generate(a.begin(), a.end(), []{return rand();});
std::cout<<"a unstorted: "<<std::endl;
displayvector(a);
duration = clock();
{
insertionSort(a, a.size());
}
duration = clock()-duration;
std::cout<<"-----------SORTED----------"<<std::endl;
displayvector(a);
std::cout<<"\n Insertion Sort algorithm Time clock ticks: "<<(((float)duration)/CLOCKS_PER_SEC)<<std::endl;
method = 'i';
printFile(method,size , duration );
//---------------------------------------Merge sort funtion--------------------------//
std::cout<<"\n-----------MERGE SORT ALGORITHM-------------"<<std::endl;
std::generate(a.begin(), a.end(), []{return rand();});
std::cout<<"a unstorted: "<<std::endl;
displayvector(a);
duration = clock();
{
mergeSort(a);
}
duration = clock()-duration;
std::cout<<"-----------SORTED----------"<<std::endl;
displayvector(a);
std::cout<<"\n Merge Sort algorithm Time clock ticks: "<<(((float)duration)/CLOCKS_PER_SEC)<<std::endl;
method = 'm';
printFile(method,size , duration );
//---------------------------------------Quick sort funtion--------------------------//
std::cout<<"\n-----------QUICK SORT ALGORITHM-------------"<<std::endl;
std::generate(a.begin(), a.end(), []{return rand();});
std::cout<<"a unstorted: "<<std::endl;
displayvector(a);
duration = clock();
{
quickSort(a, 0, a.size()-1);
}
duration = clock()-duration;
std::cout<<"-----------SORTED----------"<<std::endl;
displayvector(a);
std::cout<<"\n Quick Sort algorithm Time clock ticks: "<<(((float)duration)/CLOCKS_PER_SEC)<<std::endl;
method = 'q';
printFile(method,size , duration );
}//--------------------------//end main
void displayvector(std::vector<int> value){
for (unsigned count = 0; count < value.size(); count++)
std::cout << count << ": "<< value[count] << std::endl;
}
/********************************************
* *
* STD sort *
* Function *
* *
********************************************/
void sort(std::vector<int> &a){
std::sort(a.begin(), a.end());
}
/********************************************
* *
* Inserton sort *
* Function *
* *
********************************************/
void insertionSort(std::vector<int> &vec, int size){
int i, j, temp;
for(i = 1; i < vec.size(); ++i){
temp = vec[i];
j = i - 1;
while( j >= 0 && vec[j] > temp){
vec[j + 1] = vec[j];
j = j-1;
}
vec[j+1] = temp;
}
}
void printFile(char method, int a, float b){
std::ofstream file;
file.open("file.txt", std::ios::out|std::ios::app);
file <<method<<": "<<a<<","<<(b)<<std::endl;
file.close();
}
/********************************************
* *
* Bubble sort *
* Function *
* *
********************************************/
void bubblesort(std::vector<int>& vec, int size)
{
int temp;
bool swap;
do{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (vec[count] > vec[count + 1])
{
temp = vec[count];
vec[count] = vec[count + 1];
vec[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
/********************************************
* *
* Selection sort *
* Function *
* *
********************************************/
void selectionSort(std::vector<int> &a){
int startScan, minIndex, minValue;
for(startScan = 0; startScan< (a.size()-1); ++startScan)
{
minIndex = startScan;
minValue = a[startScan];
for(int i = startScan +1; i < a.size(); ++i)
{
if(a[i] < minValue)
{
minValue = a[i];
minIndex = i;
}
}
a[minIndex] = a[startScan];
a[startScan] = minValue;
}
}
/********************************************
* *
* Merge sort *
* Function *
* *
********************************************/
void mergeSort(std::vector<int> & vec){
std::vector<int>copy(vec.size());
mergeS(vec, copy, 0, vec.size()-1);
}
void mergeS(std::vector <int>& vec, std::vector <int>©, int low, int high){
if(low<high)
{
int mid = (low + high)/2;
mergeS(vec, copy, low, mid);
mergeS(vec, copy, mid +1, high);
merge(vec, copy, low, mid, high);
}
}
void merge(std::vector<int> &vec, std::vector<int>©, int low, int mid, int high){
int i1 = low;
int i2 = mid+1;
int i;
for(i=low;i<=high;++i)
{
if(i1> mid)
copy[i] = vec[i2++];
else if(i2 > high)
copy[i] = vec[i1++];
else if(vec[i1]<vec[i2])
copy[i] = vec[i1++];
else
copy[i] = vec[i2++];
}
for(i = low; i<=high;++i){
vec[i] = copy[i];
}
}
/********************************************
* *
* Quick sort *
* Function *
* *
********************************************/
void swap(std::vector<int>& vec, int start, int end){
int temp = vec[start];
vec[start] = vec[end];
vec[end] = temp;
}
int partition(std::vector<int>&vec, int start, int end){
int part = start;
for(int i = part+1; i<=end; ++i){
if(vec[i] < vec[part])
{
swap(vec, i, part);
if(i != part+1)
{
swap(vec, i, part+1);}
part = part+1;
}
}
return part;
}
void quickSort(std::vector<int>& vec, int start, int end){
if(start >=end) return;
int part = partition(vec, start, end);
quickSort(vec, start, part-1);
quickSort(vec, part+1, end);
}