From c69758760504eeb7fa439c0cd5c91c554714a729 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Wed, 1 Apr 2020 23:46:59 -0400 Subject: [PATCH] added new shell-sort algorithm --- sorting/shell_Sort.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/sorting/shell_Sort.c b/sorting/shell_Sort.c index c332647d..d48a1ae7 100644 --- a/sorting/shell_Sort.c +++ b/sorting/shell_Sort.c @@ -37,6 +37,33 @@ 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;