aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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;
+}