revert to single function implementation of shell_sort

This commit is contained in:
Krishna Vedala 2020-04-07 12:22:31 -04:00
parent c4c2263cc6
commit 17419855cb
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7
1 changed files with 0 additions and 43 deletions

View File

@ -37,38 +37,10 @@ void shellSort(int array[], int len)
swap(&array[j], &array[j + gap]);
}
/**
* Optimized algorithm - takes half the time as other
**/
void shell_sort2(int array[], int LEN)
{
const int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
const int gap_len = 8;
int i, j, g;
for (g = 0; g < gap_len; g++)
{
int gap = gaps[g];
for (i = gap; i < LEN; i++)
{
int tmp = array[i];
for (j = i; j >= gap && (array[j - gap] - tmp) > 0; j -= gap)
array[j] = array[j - gap];
array[j] = tmp;
}
}
#ifdef DEBUG
for (i = 0; i < LEN; i++)
printf("%s\t", data[i]);
#endif
}
int main(int argc, char *argv[])
{
int i;
int array[ELEMENT_NR];
int array2[ELEMENT_NR];
int range = 500;
int size;
clock_t start, end;
@ -76,10 +48,7 @@ int main(int argc, char *argv[])
srand(time(NULL));
for (i = 0; i < ELEMENT_NR; i++)
{
array[i] = rand() % range + 1;
array2[i] = array[i];
}
size = ARRAY_LEN(array);
@ -95,17 +64,5 @@ int main(int argc, char *argv[])
printf("%s\n", notation);
printf("Time spent sorting: %.4g ms\n", time_spent * 1e3);
printf("--------------------------\n");
start = clock();
shell_sort2(array2, size);
end = clock();
time_spent = (double)(end - start) / CLOCKS_PER_SEC;
printf("Data Sorted\n");
show_data(array2, size);
printf("%s\n", notation);
printf("Time spent sorting: %.4g ms\n", time_spent * 1e3);
return 0;
}