-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSorts.cpp
More file actions
193 lines (159 loc) · 5.32 KB
/
Sorts.cpp
File metadata and controls
193 lines (159 loc) · 5.32 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
#include<cstdlib>
#include<iostream>
#include<queue>
#include<string>
#include<sys/time.h>
#include<math.h>
using namespace std;
typedef unsigned long long timestamp_t;
void printArr(int* input, int length, bool del);
int* copyArray(int* input, int k);
int* bubbleSort(int* input, int k);
int* selectionSort(int* input, int k);
int* bucketSort(int* input, int k);
int* radixSort(int* input, int k);
int ARR_LEN = 20;
static timestamp_t
get_timestamp () {
struct timeval now;
gettimeofday (&now, NULL);
return now.tv_usec + (timestamp_t)now.tv_sec * 1000000;
}
int main(){
// Initialize array with random data
srand(time(NULL));
int* array = new int[ARR_LEN];
for (int i = 0; i < ARR_LEN; i++){
array[i] = rand() % 1000 + 1;
}
cout << "Random array used for sorting is : " << endl;
printArr(array, ARR_LEN, false);
cout << endl << "Sorting array using bubbleSort..." << endl;
timestamp_t start = get_timestamp();
printArr(bubbleSort(copyArray(array, ARR_LEN), ARR_LEN), ARR_LEN, false);
cout << "time elapsed: " << (get_timestamp() - start)/1000000.0L << " second(s)" << endl;
cout << endl << "Sorting array using selectionSort..." << endl;
start = get_timestamp();
printArr(selectionSort(copyArray(array, ARR_LEN), ARR_LEN), ARR_LEN, false);
cout << "time elapsed: " << (get_timestamp() - start)/1000000.0L << " second(s)" << endl;
cout << endl;
cout << endl << "Sorting array using bucketSort..." << endl;
start = get_timestamp();
printArr(bucketSort(copyArray(array, ARR_LEN), ARR_LEN), ARR_LEN, false);
cout << "time elapsed: " << (get_timestamp() - start)/1000000.0L << " second(s)" << endl;
cout << endl;
cout << endl << "Sorting array using radixSort..." << endl;
start = get_timestamp();
printArr(radixSort(copyArray(array, ARR_LEN), ARR_LEN), ARR_LEN, false);
cout << "time elapsed: " << (get_timestamp() - start)/1000000.0L << " second(s)" << endl;
cout << endl;
}
// returns a copy of input array
int* copyArray(int* input, int k){
int* result = new int[k];
for (int i = 0; i < k; i++){
result[i] = input[i];
}
return result;
}
// Prints given array in single line
// and then deletes it.
void printArr(int* input, int length, bool del){
for (int i = 0; i < length; i++){
cout << input[i] << " ";
}
cout << endl;
if (del){
delete[] input;
input = NULL;
}
}
int* bubbleSort(int* input, int k) {
for (int i = 0; i < k-1; i++) {
for (int j = 0; j < k-1-i; j++) {
if (input[j] > input[j+1]){
// Swaps number at j with j+1 using XOR
input[j] ^= input[j+1];
input[j+1] ^= input[j];
input[j] ^= input[j+1];
}
}
}
return input;
}
int* selectionSort(int* input, int k){
int* temp = input;
for (int i = 0; i < k-1; i++){
for (int j = i+1; j < k; j++){
temp = input[j] <= *temp ? &input[j] : temp;
}
if (input[i] > *temp){
// Swaps number at i with that at temp using XOR
input[i] ^= *temp;
*temp ^= input[i];
input[i] ^= *temp;
}
// temp is assigned first value from next set
*temp = input[i+1];
}
return input;
}
// Bubble sort implementation to sort positive numbers in c++
int* bucketSort(int* input, int k){
// Finds and stores the maximum number that appears
// among the input set.
int max = *input;
for (int i = 0; i < k; i++) {
max = max > input[i] ? max : input[i];
}
// Creates new array of bucket
int* bucketArr = new int[max+1];
// Stores input into to corresponding bucket
for (int i = 0; i < k; i++){
bucketArr[input[i]]++;
}
// Array that stores result to return
int* result = new int[k];
// Iterates over every bucket and fills result
//
// tracks index of input array
// |
// | tracks index of result array
// | |
// | | equals to condition so that largest doesn't get left out
// | | |
// * * *
for (int i = 0, j = 0; i <= max && j <= max; i++){
for (int l = 0; l < bucketArr[i]; l++){
result[j++] = i;
}
}
return result;
}
// Implementation of https://www.youtube.com/watch?v=GUHGMtNo6RQ
int* radixSort(int* input, int k){
int count = 0;
// number of digits in a number is (floor(log10(number)) + 1)
int maxDigits = 0;
for (int i = 0; i < k; i++){
int digits = floor(log10(input[i])) + 1;
maxDigits = maxDigits > digits ? maxDigits : digits;
}
for (int i = 0; i < maxDigits; i++){
queue<int>* aux = new queue<int>[10];
for (int j = 0; j < k; j++){
// gets i th least significant digit of the number
int digit = (input[j]%((int)pow(10, i+1)))/(int)pow(10, i);
count++;
aux[digit].push(input[j]);
}
// Rebuilding the array
for (int j = 0, l = 0; j < 10; j++){
while (aux[j].size()){
input[l++] = aux[j].front();
aux[j].pop();
}
}
}
return input;
}