diff options
| author | Thomas Schmucker <ts@its1.de> | 2021-12-29 13:37:57 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2021-12-29 13:37:57 +0100 |
| commit | 498b794ce5bae04495d916380927e9be23591e49 (patch) | |
| tree | 2f0f4a2eb1a554c890735415c7cb00901136b9cf | |
| parent | 5e696c74c9649b0e54133d46a3ae92e5ce0632fb (diff) | |
| download | data-structures-498b794ce5bae04495d916380927e9be23591e49.tar.gz data-structures-498b794ce5bae04495d916380927e9be23591e49.tar.bz2 data-structures-498b794ce5bae04495d916380927e9be23591e49.zip | |
feat: implement insertsort
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | insertsort.c | 38 | ||||
| -rw-r--r-- | makefile | 7 |
3 files changed, 44 insertions, 2 deletions
| @@ -16,4 +16,5 @@ stack | |||
| 16 | stack2 | 16 | stack2 |
| 17 | tags | 17 | tags |
| 18 | tree | 18 | tree |
| 19 | insertsort | ||
| 19 | 20 | ||
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 | } | ||
| @@ -2,7 +2,7 @@ | |||
| 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 | 4 | ||
| 5 | all: list list-tail-node dlist stack stack2 queue ringbuff hashtab tree avl heap quicksort allocator rb deque arraylist | 5 | all: list list-tail-node dlist stack stack2 queue ringbuff hashtab tree avl heap quicksort allocator rb deque arraylist insertsort |
| 6 | 6 | ||
| 7 | list: list.c util.h | 7 | list: list.c util.h |
| 8 | cc $(CFLAGS) $< -o $@ | 8 | cc $(CFLAGS) $< -o $@ |
| @@ -52,6 +52,9 @@ allocator: allocator.c allocator.h | |||
| 52 | arraylist: arraylist.c util.h | 52 | arraylist: arraylist.c util.h |
| 53 | cc $(CFLAGS) $< -o $@ | 53 | cc $(CFLAGS) $< -o $@ |
| 54 | 54 | ||
| 55 | insertsort: insertsort.c util.h | ||
| 56 | cc $(CFLAGS) $< -o $@ | ||
| 57 | |||
| 55 | clean: | 58 | clean: |
| 56 | rm -f list list-tail-node dlist stack stack2 queue ringbuff hashtab tree avl heap quicksort allocator rb deque arraylist | 59 | rm -f list list-tail-node dlist stack stack2 queue ringbuff hashtab tree avl heap quicksort allocator rb deque arraylist insertsort |
| 57 | 60 | ||
