summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/rg_ssort.c
diff options
context:
space:
mode:
Diffstat (limited to 'reference/C/CONTRIB/SNIP/rg_ssort.c')
-rwxr-xr-xreference/C/CONTRIB/SNIP/rg_ssort.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/reference/C/CONTRIB/SNIP/rg_ssort.c b/reference/C/CONTRIB/SNIP/rg_ssort.c
new file mode 100755
index 0000000..b4a7675
--- /dev/null
+++ b/reference/C/CONTRIB/SNIP/rg_ssort.c
@@ -0,0 +1,43 @@
+/*
+** ssort() -- Fast, small, qsort()-compatible Shell sort
+**
+** by Ray Gardner, public domain 5/90
+*/
+
+#include <stddef.h>
+
+void ssort (void *base,
+ size_t nel,
+ size_t width,
+ int (*comp)(const void *, const void *))
+{
+ size_t wnel, gap, wgap, i, j, k;
+ char *a, *b, tmp;
+
+ wnel = width * nel;
+ for (gap = 0; ++gap < nel;)
+ gap *= 3;
+ while ( gap /= 3 )
+ {
+ wgap = width * gap;
+ for (i = wgap; i < wnel; i += width)
+ {
+ for (j = i - wgap; ;j -= wgap)
+ {
+ a = j + (char *)base;
+ b = a + wgap;
+ if ( (*comp)(a, b) <= 0 )
+ break;
+ k = width;
+ do
+ {
+ tmp = *a;
+ *a++ = *b;
+ *b++ = tmp;
+ } while ( --k );
+ if (j < wgap)
+ break;
+ }
+ }
+ }
+}