diff options
author | sinanmohd <sinan@firemail.cc> | 2023-05-09 08:43:52 +0530 |
---|---|---|
committer | sinanmohd <sinan@firemail.cc> | 2023-05-09 08:44:43 +0530 |
commit | 2bc5c4c55b979af164c0bf7539e4f0bae6c263e8 (patch) | |
tree | e1cffbfda704d8299e959f27140cbb80d6137551 | |
parent | 338b2237b3d00745469fb265d77638ec245ca367 (diff) |
shell sort: initial commit
-rw-r--r-- | ssort.c | 45 |
1 files changed, 45 insertions, 0 deletions
@@ -0,0 +1,45 @@ +#include <stdio.h> + +#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; +} |