c-random/gnome-sort.c
joachimschmidt557 abf965e7ce Use swap macro
2019-05-02 15:16:44 +02:00

25 lines
493 B
C

#include <stdio.h>
#define SWAP(x, y, T) do { T SWAP = x; x = y; y = SWAP; } while (0)
void gnome_sort(int array[], int n) {
int i = 0;
while (i < n) {
if (array[i] > array[i+1]) {
SWAP(array[i], array[i+1], int);
if (i > 0) i--;
}
else i++;
}
}
void print_array(int array[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", array[i]);
}
void main() {
int array[] = {1, 3, 6, 2, 4, 73, 23, 61, 32, 64, 11, 32, 3, 2};
gnome_sort(array, 13);
print_array(array, 13);
}