-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsortOfAllSort.cpp
More file actions
185 lines (153 loc) · 4.56 KB
/
sortOfAllSort.cpp
File metadata and controls
185 lines (153 loc) · 4.56 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
#include <iostream>
#include <bits/stdc++.h>
#include <time.h>
#include <chrono>
using namespace std;
using namespace std::chrono;
// Prints array upto (n - 1) index.
void print(int A[], int n) {
for(int i = 0; i < n; i++) {
cout << A[i] << " ";
}
cout << endl;
}
// Swap's value of two integer pointers.
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
// Loops n times and compares adjacent elements.
// If the comparison has a higher value on left, a swap is performed.
void bubbleSort(int A[],int n) {
// Loop n times
for(int k = 1; k < n; k++) {
// Flag tracks if no swaps are made in the upcoming loop
int flag = 0;
for(int i = 0; i < n - k; i++) {
// Swap if comparison is unordered
if(A[i] > A[i + 1]) {
swap(&A[i], &A[i + 1]);
// Swap done, so mark flag
flag = true;
}
// Flag of 0 means no swaps, which means list is already sorted
if(flag == 0) {
break;
}
}
}
// Print sorted array
print(A, n);
}
// Loops n times and moves smallest value to the beginning (of unsorted part).
// Challenge: Add a flag that accomplishes the same goal as in bubbleSort
void selectionSort(int A[],int n) {
for(int i = 0; i < n - 1; i++) {
// Holds the index of the minimum value
int min_index = i;
// Loops through unsorted part and update min_index
for(int j = i + 1; j < n; j++) {
if(A[j] < A[min_index]) {
min_index = j;
}
}
// Loop is done, so swap the min_index value with the one at the start (of the unsorted part)
swap(&A[i], &A[min_index]);
}
// Print sorted array
print(A, n);
}
// Loops n times and moves current loop-iteration's value down to the correct location.
void insertionSort(int A[], int n) {
for(int i = 1; i < n; i++) {
// Move the current value through sorted values into correct location.
for(int j = i; j != 0; j--) {
// Sorted value is higher than selected value, so swap
if(A[j] < A[j - 1]) {
swap(&A[j], &A[j - 1]);
}
// Since sorted value is lower (or equal), break because correct location reached.
else {
break;
}
}
}
// Print sorted array
print(A, n);
}
int partition(int A[], int begin, int end) {
int pivot = A[end];
int index = begin;
for(int i = begin; i < end; i++) {
if(A[i] <= pivot) {
swap(&A[i], &A[index]);
index++;
}
}
swap(&A[end], &A[index]);
return index;
}
// Paritions array into two based off of a pivot index. One holds values less than pivot,
// the other holds values higher than pivot. Then recursively sorts the two arrays.
void quickSort(int *arr, int begin, int end) {
int index;
if(begin < end) {
index = partition(arr, begin, end);
quickSort(arr, begin, index - 1);
quickSort(arr, index + 1, end);
}
}
// Randomly swaps elements in array.
void randomize(int arr[], int n) {
srand(time(NULL));
for(int i = n - 1; i > 0; i--) {
int j = rand() % (i + 1);
swap(&arr[i], &arr[j]);
}
}
int main() {
// Read number of elements.
int n;
cin >> n;
// Array for holding values.
int A[n];
// Read elements into both arrays
for(int i = 0; i < n; i++) {
cin >> A[i];
}
// Selection Sort
auto start_ss = high_resolution_clock::now(); // Start time
selectionSort(A, n);
auto stop_ss = high_resolution_clock::now(); // End time
auto duration_ss = duration_cast<microseconds>(stop_ss - start_ss);
cout << "The time for Selection Sort " << duration_ss.count() << " microseconds\n";
// Randomize and print array
randomize(A, n);
print(A, n);
// Insertion Sort
auto start_is = high_resolution_clock::now();
insertionSort(A, n);
auto stop_is = high_resolution_clock::now();
auto duration_is = duration_cast<microseconds>(stop_is - start_is);
cout << "The time for Insertion Sort " << duration1.count() << " microseconds\n";
// Randomize and print array
randomize(A, n);
print(A, n);
auto start_bs = high_resolution_clock::now();
bubbleSort(A, n);
auto stop_bs = high_resolution_clock::now();
auto duration_bs = duration_cast<microseconds>(stop_bs - start_bs);
cout << "The time for Bubble Sort " << duration_bs.count() << " microseconds\n";
// Randomize and print array
randomize(A, n);
print(A, n);
// Quick Sort
auto start_qs = high_resolution_clock::now();
quickSort(A, 0, n - 1);
auto stop_qs = high_resolution_clock::now();
auto duration_qs = duration_cast<microseconds>(stop_qs - start_qs);
print(A, n);
cout << "The time for Quick Sort " << duration_qs.count() << " microseconds\n";
return 0;
}