diff options
Diffstat (limited to 'anim/tools/sort.c')
| -rw-r--r-- | anim/tools/sort.c | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/anim/tools/sort.c b/anim/tools/sort.c new file mode 100644 index 0000000..5816390 --- /dev/null +++ b/anim/tools/sort.c @@ -0,0 +1,22 @@ +int a[8] = {4, 9, 17, 31, 12, 26, 5, 20}; + +void swap(int *x, int *y) { + int t = *x; + *x = *y; + *y = t; +} + +void sort(int n) { + for (int i = 1; i < n; i++) { + int j = i; + while (j > 0 && a[j - 1] > a[j]) { + swap(&a[j - 1], &a[j]); + j--; + } + } +} + +int main(void) { + sort(8); + return 0; +} |
