TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
shell_sort2.cpp
Go to the documentation of this file.
1
6#include <cassert>
7#include <cstdlib>
8#include <ctime>
9#include <iostream>
10#include <utility> // for std::swap
11#include <vector>
12
17template <class T>
18void show_data(T *arr, size_t LEN) {
19 size_t i;
20
21 for (i = 0; i < LEN; i++) {
22 std::cout << arr[i] << ", ";
23 }
24 std::cout << std::endl;
25}
26
31template <typename T, size_t N>
32void show_data(T (&arr)[N]) {
33 show_data(arr, N);
34}
35
39namespace sorting {
44template <typename T>
45void shell_sort(T *arr, size_t LEN) {
46 const unsigned int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
47 const unsigned int gap_len = 8;
48 size_t i, j, g;
49
50 for (g = 0; g < gap_len; g++) {
51 unsigned int gap = gaps[g];
52 for (i = gap; i < LEN; i++) {
53 T tmp = arr[i];
54
55 for (j = i; j >= gap && (arr[j - gap] - tmp) > 0; j -= gap) {
56 arr[j] = arr[j - gap];
57 }
58
59 arr[j] = tmp;
60 }
61 }
62}
63
66template <typename T, size_t N>
67void shell_sort(T (&arr)[N]) {
68 shell_sort(arr, N);
69}
70
74template <typename T>
75void shell_sort(std::vector<T> *arr) {
76 shell_sort(arr->data(), arr->size());
77}
78
79} // namespace sorting
80
82
86template <typename T>
87int compare(const void *a, const void *b) {
88 T arg1 = *static_cast<const T *>(a);
89 T arg2 = *static_cast<const T *>(b);
90
91 if (arg1 < arg2)
92 return -1;
93 if (arg1 > arg2)
94 return 1;
95 return 0;
96
97 // return (arg1 > arg2) - (arg1 < arg2); // possible shortcut
98 // return arg1 - arg2; // erroneous shortcut (fails if INT_MIN is present)
99}
100
105void test_int(const int NUM_DATA) {
106 // int array = new int[NUM_DATA];
107 int *data = new int[NUM_DATA];
108 int *data2 = new int[NUM_DATA];
109 // int array2 = new int[NUM_DATA];
110 int range = 1800;
111
112 for (int i = 0; i < NUM_DATA; i++)
113 data[i] = data2[i] = (std::rand() % range) - (range >> 1);
114
115 /* sort using our implementation */
116 std::clock_t start = std::clock();
117 shell_sort(data, NUM_DATA);
118 std::clock_t end = std::clock();
119 double elapsed_time = static_cast<double>(end - start) / CLOCKS_PER_SEC;
120 std::cout << "Time spent sorting using shell_sort2: " << elapsed_time
121 << "s\n";
122
123 /* sort using std::qsort */
124 start = std::clock();
125 std::qsort(data2, NUM_DATA, sizeof(data2[0]), compare<int>);
126 end = std::clock();
127
128 elapsed_time = static_cast<double>(end - start) / CLOCKS_PER_SEC;
129 std::cout << "Time spent sorting using std::qsort: " << elapsed_time
130 << "s\n";
131
132 for (int i = 0; i < NUM_DATA; i++) {
133 assert(data[i] == data2[i]); // ensure that our sorting results match
134 // the standard results
135 }
136
137 delete[] data;
138 delete[] data2;
139}
140
145void test_f(const int NUM_DATA) {
146 // int array = new int[NUM_DATA];
147 float *data = new float[NUM_DATA];
148 float *data2 = new float[NUM_DATA];
149 // int array2 = new int[NUM_DATA];
150 int range = 1000;
151
152 for (int i = 0; i < NUM_DATA; i++) {
153 data[i] = data2[i] = ((std::rand() % range) - (range >> 1)) / 100.;
154 }
155
156 /* sort using our implementation */
157 std::clock_t start = std::clock();
158 shell_sort(data, NUM_DATA);
159 std::clock_t end = std::clock();
160 double elapsed_time = static_cast<double>(end - start) / CLOCKS_PER_SEC;
161 std::cout << "Time spent sorting using shell_sort2: " << elapsed_time
162 << "s\n";
163
164 /* sort using std::qsort */
165 start = std::clock();
166 std::qsort(data2, NUM_DATA, sizeof(data2[0]), compare<float>);
167 end = std::clock();
168
169 elapsed_time = static_cast<double>(end - start) / CLOCKS_PER_SEC;
170 std::cout << "Time spent sorting using std::qsort: " << elapsed_time
171 << "s\n";
172
173 for (int i = 0; i < NUM_DATA; i++) {
174 assert(data[i] == data2[i]); // ensure that our sorting results match
175 // the standard results
176 }
177
178 delete[] data;
179 delete[] data2;
180}
181
183int main(int argc, char *argv[]) {
184 // initialize random number generator - once per program
185 std::srand(std::time(NULL));
186
187 test_int(100); // test with sorting random array of 100 values
188 std::cout << "Test 1 - 100 int values - passed. \n";
189 test_int(1000); // test with sorting random array of 1000 values
190 std::cout << "Test 2 - 1000 int values - passed.\n";
191 test_int(10000); // test with sorting random array of 10000 values
192 std::cout << "Test 3 - 10000 int values - passed.\n";
193
194 test_f(100); // test with sorting random array of 100 values
195 std::cout << "Test 1 - 100 float values - passed. \n";
196 test_f(1000); // test with sorting random array of 1000 values
197 std::cout << "Test 2 - 1000 float values - passed.\n";
198 test_f(10000); // test with sorting random array of 10000 values
199 std::cout << "Test 3 - 10000 float values - passed.\n";
200
201 int i, NUM_DATA;
202
203 if (argc == 2)
204 NUM_DATA = atoi(argv[1]);
205 else
206 NUM_DATA = 200;
207
208 // int array = new int[NUM_DATA];
209 int *data = new int[NUM_DATA];
210 // int array2 = new int[NUM_DATA];
211 int range = 1800;
212
213 std::srand(time(NULL));
214 for (i = 0; i < NUM_DATA; i++) {
215 // allocate random numbers in the given range
216 data[i] = (std::rand() % range) - (range >> 1);
217 }
218
219 std::cout << "Unsorted original data: " << std::endl;
220 show_data(data, NUM_DATA);
221 std::clock_t start = std::clock();
222 shell_sort(data, NUM_DATA); // perform sorting
223 std::clock_t end = std::clock();
224
225 std::cout << std::endl
226 << "Data Sorted using custom implementation: " << std::endl;
227 show_data(data, NUM_DATA);
228
229 double elapsed_time = (end - start) * 1.f / CLOCKS_PER_SEC;
230 std::cout << "Time spent sorting: " << elapsed_time << "s\n" << std::endl;
231
232 delete[] data;
233 return 0;
234}
int main()
Main function.
int data[MAX]
test data
for working with vectors
void shell_sort(T *arr, size_t LEN)
static void test_int()
void test_f(const int NUM_DATA)
int compare(const void *a, const void *b)
void show_data(T *arr, size_t LEN)