aboutsummaryrefslogtreecommitdiff
path: root/ssort.c
diff options
context:
space:
mode:
authorsinanmohd <sinan@firemail.cc>2023-05-09 08:43:52 +0530
committersinanmohd <sinan@firemail.cc>2023-05-09 08:44:43 +0530
commit2bc5c4c55b979af164c0bf7539e4f0bae6c263e8 (patch)
treee1cffbfda704d8299e959f27140cbb80d6137551 /ssort.c
parent338b2237b3d00745469fb265d77638ec245ca367 (diff)
shell sort: initial commit
Diffstat (limited to 'ssort.c')
-rw-r--r--ssort.c45
1 files changed, 45 insertions, 0 deletions
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 <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;
+}