diff options
| author | Thomas Schmucker <ts@its1.de> | 2022-04-09 09:43:53 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2022-04-09 09:43:53 +0200 |
| commit | 154874afda4a8df885e51c01f7681f04fb0b8e61 (patch) | |
| tree | 274817eb2793584b0e3d856de5504a614f2b4e89 /src/insertsort.c | |
| parent | 2863f4f1d2a6a6a8704454824d6c291d2e0d4b5c (diff) | |
| download | data-structures-154874afda4a8df885e51c01f7681f04fb0b8e61.tar.gz data-structures-154874afda4a8df885e51c01f7681f04fb0b8e61.tar.bz2 data-structures-154874afda4a8df885e51c01f7681f04fb0b8e61.zip | |
neue Verzeichnisstruktur
Diffstat (limited to 'src/insertsort.c')
| -rw-r--r-- | src/insertsort.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/insertsort.c b/src/insertsort.c new file mode 100644 index 0000000..1a30a1a --- /dev/null +++ b/src/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 | } | ||
