-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
shell_sort2.cpp
234 lines (195 loc) · 6.87 KB
/
shell_sort2.cpp
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
/**
* \file
* \brief [Shell sort](https://en.wikipedia.org/wiki/Shell_sort) algorithm
* \author [Krishna Vedala](https://github.com/kvedala)
*/
#include <cassert>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <utility> // for std::swap
#include <vector>
/** pretty print array
* \param[in] arr array to print
* \param[in] LEN length of array to print
*/
template <class T>
void show_data(T *arr, size_t LEN) {
size_t i;
for (i = 0; i < LEN; i++) {
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
}
/** pretty print array
* \param[in] arr array to print
* \param[in] N length of array to print
*/
template <typename T, size_t N>
void show_data(T (&arr)[N]) {
show_data(arr, N);
}
/** \namespace sorting
* \brief Sorting algorithms
*/
namespace sorting {
/**
* Optimized algorithm - takes half the time by utilizing
* Mar
**/
template <typename T>
void shell_sort(T *arr, size_t LEN) {
const unsigned int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
const unsigned int gap_len = 8;
size_t i, j, g;
for (g = 0; g < gap_len; g++) {
unsigned int gap = gaps[g];
for (i = gap; i < LEN; i++) {
T tmp = arr[i];
for (j = i; j >= gap && (arr[j - gap] - tmp) > 0; j -= gap) {
arr[j] = arr[j - gap];
}
arr[j] = tmp;
}
}
}
/** function overload - when input array is of a known length array type
*/
template <typename T, size_t N>
void shell_sort(T (&arr)[N]) {
shell_sort(arr, N);
}
/** function overload - when input array is of type std::vector,
* simply send the data content and the data length to the above function.
*/
template <typename T>
void shell_sort(std::vector<T> *arr) {
shell_sort(arr->data(), arr->size());
}
} // namespace sorting
using sorting::shell_sort;
/**
* function to compare sorting using cstdlib's qsort
**/
template <typename T>
int compare(const void *a, const void *b) {
T arg1 = *static_cast<const T *>(a);
T arg2 = *static_cast<const T *>(b);
if (arg1 < arg2)
return -1;
if (arg1 > arg2)
return 1;
return 0;
// return (arg1 > arg2) - (arg1 < arg2); // possible shortcut
// return arg1 - arg2; // erroneous shortcut (fails if INT_MIN is present)
}
/**
* Test implementation of shell_sort on integer arrays by comparing results
* against std::qsort.
*/
void test_int(const int NUM_DATA) {
// int array = new int[NUM_DATA];
int *data = new int[NUM_DATA];
int *data2 = new int[NUM_DATA];
// int array2 = new int[NUM_DATA];
int range = 1800;
for (int i = 0; i < NUM_DATA; i++)
data[i] = data2[i] = (std::rand() % range) - (range >> 1);
/* sort using our implementation */
std::clock_t start = std::clock();
shell_sort(data, NUM_DATA);
std::clock_t end = std::clock();
double elapsed_time = static_cast<double>(end - start) / CLOCKS_PER_SEC;
std::cout << "Time spent sorting using shell_sort2: " << elapsed_time
<< "s\n";
/* sort using std::qsort */
start = std::clock();
std::qsort(data2, NUM_DATA, sizeof(data2[0]), compare<int>);
end = std::clock();
elapsed_time = static_cast<double>(end - start) / CLOCKS_PER_SEC;
std::cout << "Time spent sorting using std::qsort: " << elapsed_time
<< "s\n";
for (int i = 0; i < NUM_DATA; i++) {
assert(data[i] == data2[i]); // ensure that our sorting results match
// the standard results
}
delete[] data;
delete[] data2;
}
/**
* Test implementation of shell_sort on float arrays by comparing results
* against std::qsort.
*/
void test_f(const int NUM_DATA) {
// int array = new int[NUM_DATA];
float *data = new float[NUM_DATA];
float *data2 = new float[NUM_DATA];
// int array2 = new int[NUM_DATA];
int range = 1000;
for (int i = 0; i < NUM_DATA; i++) {
data[i] = data2[i] = ((std::rand() % range) - (range >> 1)) / 100.;
}
/* sort using our implementation */
std::clock_t start = std::clock();
shell_sort(data, NUM_DATA);
std::clock_t end = std::clock();
double elapsed_time = static_cast<double>(end - start) / CLOCKS_PER_SEC;
std::cout << "Time spent sorting using shell_sort2: " << elapsed_time
<< "s\n";
/* sort using std::qsort */
start = std::clock();
std::qsort(data2, NUM_DATA, sizeof(data2[0]), compare<float>);
end = std::clock();
elapsed_time = static_cast<double>(end - start) / CLOCKS_PER_SEC;
std::cout << "Time spent sorting using std::qsort: " << elapsed_time
<< "s\n";
for (int i = 0; i < NUM_DATA; i++) {
assert(data[i] == data2[i]); // ensure that our sorting results match
// the standard results
}
delete[] data;
delete[] data2;
}
/** Main function */
int main(int argc, char *argv[]) {
// initialize random number generator - once per program
std::srand(std::time(NULL));
test_int(100); // test with sorting random array of 100 values
std::cout << "Test 1 - 100 int values - passed. \n";
test_int(1000); // test with sorting random array of 1000 values
std::cout << "Test 2 - 1000 int values - passed.\n";
test_int(10000); // test with sorting random array of 10000 values
std::cout << "Test 3 - 10000 int values - passed.\n";
test_f(100); // test with sorting random array of 100 values
std::cout << "Test 1 - 100 float values - passed. \n";
test_f(1000); // test with sorting random array of 1000 values
std::cout << "Test 2 - 1000 float values - passed.\n";
test_f(10000); // test with sorting random array of 10000 values
std::cout << "Test 3 - 10000 float values - passed.\n";
int i, NUM_DATA;
if (argc == 2)
NUM_DATA = atoi(argv[1]);
else
NUM_DATA = 200;
// int array = new int[NUM_DATA];
int *data = new int[NUM_DATA];
// int array2 = new int[NUM_DATA];
int range = 1800;
std::srand(time(NULL));
for (i = 0; i < NUM_DATA; i++) {
// allocate random numbers in the given range
data[i] = (std::rand() % range) - (range >> 1);
}
std::cout << "Unsorted original data: " << std::endl;
show_data(data, NUM_DATA);
std::clock_t start = std::clock();
shell_sort(data, NUM_DATA); // perform sorting
std::clock_t end = std::clock();
std::cout << std::endl
<< "Data Sorted using custom implementation: " << std::endl;
show_data(data, NUM_DATA);
double elapsed_time = (end - start) * 1.f / CLOCKS_PER_SEC;
std::cout << "Time spent sorting: " << elapsed_time << "s\n" << std::endl;
delete[] data;
return 0;
}