diff options
| author | Thomas Schmucker <ts@its1.de> | 2022-01-01 13:42:26 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2022-01-01 13:42:26 +0100 |
| commit | e5442c9cb145f2aba28b1ec84389922c6de6bced (patch) | |
| tree | af183a6b8f7645b7b2ad7f7d58b51f903b3a55d2 | |
| parent | 214f0923ba2981649f14ec3761662332df446d9d (diff) | |
| parent | 20ad66e839dbc632b844f1c03137efa1a457f5fd (diff) | |
| download | data-structures-e5442c9cb145f2aba28b1ec84389922c6de6bced.tar.gz data-structures-e5442c9cb145f2aba28b1ec84389922c6de6bced.tar.bz2 data-structures-e5442c9cb145f2aba28b1ec84389922c6de6bced.zip | |
Merge branch 'feat/implement-new-sorting-algorithms'
| -rw-r--r-- | .gitignore | 3 | ||||
| -rw-r--r-- | insertsort.c | 38 | ||||
| -rw-r--r-- | makefile | 11 | ||||
| -rw-r--r-- | shellsort.c | 64 |
4 files changed, 113 insertions, 3 deletions
| @@ -16,4 +16,5 @@ stack | |||
| 16 | stack2 | 16 | stack2 |
| 17 | tags | 17 | tags |
| 18 | tree | 18 | tree |
| 19 | 19 | insertsort | |
| 20 | shellsort | ||
diff --git a/insertsort.c b/insertsort.c new file mode 100644 index 0000000..1a30a1a --- /dev/null +++ b/insertsort.c | |||
| @@ -0,0 +1,38 @@ | |||
| 1 | #include <stdio.h> | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include <time.h> | ||
| 4 | |||
| 5 | #include "util.h" | ||
| 6 | |||
| 7 | typedef int T; | ||
| 8 | |||
| 9 | void | ||
| 10 | insertsort(T a[], size_t n) | ||
| 11 | { | ||
| 12 | for ( size_t i = 1; i < n; ++i ) { | ||
| 13 | size_t j = i; | ||
| 14 | const T value = a[i]; | ||
| 15 | |||
| 16 | for ( ; j > 0 && a[j - 1] > value; --j ) | ||
| 17 | a[j] = a[j - 1]; | ||
| 18 | |||
| 19 | a[j] = value; | ||
| 20 | } | ||
| 21 | } | ||
| 22 | |||
| 23 | int | ||
| 24 | main(void) | ||
| 25 | { | ||
| 26 | T a[10]; | ||
| 27 | |||
| 28 | srand(time(NULL)); | ||
| 29 | for ( size_t i = 0; i != NELEM(a); ++i ) | ||
| 30 | a[i] = rand() % 100; | ||
| 31 | |||
| 32 | insertsort(a, NELEM(a)); | ||
| 33 | |||
| 34 | for ( size_t i = 0; i != NELEM(a); ++i ) | ||
| 35 | printf("%d ", a[i]); | ||
| 36 | putchar('\n'); | ||
| 37 | return EXIT_SUCCESS; | ||
| 38 | } | ||
| @@ -1,8 +1,9 @@ | |||
| 1 | .PHONY: all clean | 1 | .PHONY: all clean |
| 2 | 2 | ||
| 3 | CFLAGS=-Wall -Werror -Wextra -Wno-unused-function -Wsign-conversion -pedantic -std=c99 -g | 3 | CFLAGS=-Wall -Werror -Wextra -Wno-unused-function -Wsign-conversion -pedantic -std=c99 -g |
| 4 | BINS=list list-tail-node dlist stack stack2 queue ringbuff hashtab tree avl heap quicksort allocator rb deque arraylist insertsort shellsort | ||
| 4 | 5 | ||
| 5 | all: list list-tail-node dlist stack stack2 queue ringbuff hashtab tree avl heap quicksort allocator rb deque arraylist | 6 | all: $(BINS) |
| 6 | 7 | ||
| 7 | list: list.c util.h | 8 | list: list.c util.h |
| 8 | cc $(CFLAGS) $< -o $@ | 9 | cc $(CFLAGS) $< -o $@ |
| @@ -52,6 +53,12 @@ allocator: allocator.c allocator.h | |||
| 52 | arraylist: arraylist.c util.h | 53 | arraylist: arraylist.c util.h |
| 53 | cc $(CFLAGS) $< -o $@ | 54 | cc $(CFLAGS) $< -o $@ |
| 54 | 55 | ||
| 56 | insertsort: insertsort.c util.h | ||
| 57 | cc $(CFLAGS) $< -o $@ | ||
| 58 | |||
| 59 | shellsort: shellsort.c util.h | ||
| 60 | cc $(CFLAGS) $< -o $@ | ||
| 61 | |||
| 55 | clean: | 62 | clean: |
| 56 | rm -f list list-tail-node dlist stack stack2 queue ringbuff hashtab tree avl heap quicksort allocator rb deque arraylist | 63 | rm -f $(BINS) |
| 57 | 64 | ||
diff --git a/shellsort.c b/shellsort.c new file mode 100644 index 0000000..b19f547 --- /dev/null +++ b/shellsort.c | |||
| @@ -0,0 +1,64 @@ | |||
| 1 | #include <stdio.h> | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include <time.h> | ||
| 4 | |||
| 5 | #include "util.h" | ||
| 6 | |||
| 7 | typedef int T; | ||
| 8 | |||
| 9 | void | ||
| 10 | shellsort(T a[], size_t n) | ||
| 11 | { | ||
| 12 | //static const size_t gaps[] = { 701, 301, 132, 57, 23, 10, 4, 1 }; | ||
| 13 | |||
| 14 | // Wikipedia: | ||
| 15 | //static const size_t gaps[] = { 2147483647, 1131376761, 410151271, 157840433, 58548857, 21521774, 8810089, 3501671, 1355339, 543749, 213331, 84801, 27901, 11969, 4711, 1968, 815, 271, 111, 41, 13, 4, 1 }; | ||
| 16 | |||
| 17 | // https://www.cs.princeton.edu/~rs/shell/paperF.pdf | ||
| 18 | static const size_t gaps[] = { 1391376, 463792, 198768, 86961, 33936, 13776, 4592, 1968, 861, 336, 112, 48, 21, 7, 3, 1 }; | ||
| 19 | |||
| 20 | for ( size_t k = 0; k < NELEM(gaps); ++k ) { | ||
| 21 | const size_t gap = gaps[k]; | ||
| 22 | |||
| 23 | for ( size_t i = gap; i < n; i++ ) { | ||
| 24 | size_t j = i; | ||
| 25 | T temp = a[i]; | ||
| 26 | |||
| 27 | for ( ; j >= gap && a[j - gap] > temp; j -= gap ) | ||
| 28 | a[j] = a[j - gap]; | ||
| 29 | |||
| 30 | a[j] = temp; | ||
| 31 | } | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | static void | ||
| 36 | test(T a[], size_t n) | ||
| 37 | { | ||
| 38 | for ( size_t i = 1; i < n; ++i ) { | ||
| 39 | if ( a[i - 1] > a[i] ) { | ||
| 40 | fprintf(stderr, "Error\n"); | ||
| 41 | exit(EXIT_FAILURE); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | int | ||
| 47 | main(void) | ||
| 48 | { | ||
| 49 | T a[1000000]; | ||
| 50 | |||
| 51 | srand(time(NULL)); | ||
| 52 | for ( size_t i = 0; i != NELEM(a); ++i ) | ||
| 53 | a[i] = rand() % 1000; | ||
| 54 | |||
| 55 | clock_t start = clock(); | ||
| 56 | shellsort(a, NELEM(a)); | ||
| 57 | clock_t end = clock(); | ||
| 58 | |||
| 59 | test(a, NELEM(a)); | ||
| 60 | |||
| 61 | printf("duration: %.3f sec\n", ((double) (end - start)) / CLOCKS_PER_SEC); | ||
| 62 | |||
| 63 | return EXIT_SUCCESS; | ||
| 64 | } | ||
