mirror of https://github.com/TheAlgorithms/C
revert to single function implementation of shell_sort
This commit is contained in:
parent
c4c2263cc6
commit
17419855cb
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue