From 2bc5c4c55b979af164c0bf7539e4f0bae6c263e8 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Tue, 9 May 2023 08:43:52 +0530 Subject: shell sort: initial commit --- ssort.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 ssort.c diff --git a/ssort.c b/ssort.c new file mode 100644 index 0000000..4abea67 --- /dev/null +++ b/ssort.c @@ -0,0 +1,45 @@ +#include + +#define ARR_SIZE(X) (sizeof(X)/sizeof(X[0])) + +void print_array(int array[], int len); +void shell_sort(int array[], int len); +void swap(int *to, int *from); + +int main(void) +{ + int array[] = {4, 2, 1, 7, 8, 9, 6, 0, 3, 5}; + + print_array(array, sizeof(array)/sizeof(int)); + shell_sort(array, sizeof(array)/sizeof(array[0])); + print_array(array, sizeof(array)/sizeof(int)); + + return 0; +} + +void print_array(int array[], int len) +{ + while (len--) + printf((!len) ? "%d\n" : "%d, ", *array++); +} + +void shell_sort(int array[], int len) +{ + int gap, i, j; + + for (gap = len/2; gap > 0; gap /= 2) + for (i = gap; i < len; ++i) + for (j = i - gap; j >= 0 && array[j] >= array[j+gap]; + j -= gap) + swap(array + j, array + j + gap); +} + + + +void swap(int *to, int *from) +{ + int buff = *to; + + *to = *from; + *from = buff; +} -- cgit v1.2.3