46 const unsigned int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
47 const unsigned int gap_len = 8;
50 for (g = 0; g < gap_len; g++) {
51 unsigned int gap = gaps[g];
52 for (i = gap; i < LEN; i++) {
55 for (j = i; j >= gap && (arr[j - gap] - tmp) > 0; j -= gap) {
56 arr[j] = arr[j - gap];
107 int *
data =
new int[NUM_DATA];
108 int *data2 =
new int[NUM_DATA];
112 for (
int i = 0; i < NUM_DATA; i++)
113 data[i] = data2[i] = (std::rand() % range) - (range >> 1);
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
124 start = std::clock();
125 std::qsort(data2, NUM_DATA,
sizeof(data2[0]),
compare<int>);
128 elapsed_time =
static_cast<double>(end - start) / CLOCKS_PER_SEC;
129 std::cout <<
"Time spent sorting using std::qsort: " << elapsed_time
132 for (
int i = 0; i < NUM_DATA; i++) {
133 assert(
data[i] == data2[i]);
147 float *
data =
new float[NUM_DATA];
148 float *data2 =
new float[NUM_DATA];
152 for (
int i = 0; i < NUM_DATA; i++) {
153 data[i] = data2[i] = ((std::rand() % range) - (range >> 1)) / 100.;
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
165 start = std::clock();
169 elapsed_time =
static_cast<double>(end - start) / CLOCKS_PER_SEC;
170 std::cout <<
"Time spent sorting using std::qsort: " << elapsed_time
173 for (
int i = 0; i < NUM_DATA; i++) {
174 assert(
data[i] == data2[i]);
183int main(
int argc,
char *argv[]) {
185 std::srand(std::time(NULL));
188 std::cout <<
"Test 1 - 100 int values - passed. \n";
190 std::cout <<
"Test 2 - 1000 int values - passed.\n";
192 std::cout <<
"Test 3 - 10000 int values - passed.\n";
195 std::cout <<
"Test 1 - 100 float values - passed. \n";
197 std::cout <<
"Test 2 - 1000 float values - passed.\n";
199 std::cout <<
"Test 3 - 10000 float values - passed.\n";
204 NUM_DATA = atoi(argv[1]);
209 int *
data =
new int[NUM_DATA];
213 std::srand(time(NULL));
214 for (i = 0; i < NUM_DATA; i++) {
216 data[i] = (std::rand() % range) - (range >> 1);
219 std::cout <<
"Unsorted original data: " << std::endl;
221 std::clock_t start = std::clock();
222 shell_sort(
data, NUM_DATA);
223 std::clock_t end = std::clock();
225 std::cout << std::endl
226 <<
"Data Sorted using custom implementation: " << std::endl;
229 double elapsed_time = (end - start) * 1.f / CLOCKS_PER_SEC;
230 std::cout <<
"Time spent sorting: " << elapsed_time <<
"s\n" << std::endl;