Use swap macro

This commit is contained in:
joachimschmidt557 2019-05-02 15:16:44 +02:00
parent d78469d21a
commit abf965e7ce

View file

@ -1,19 +1,12 @@
#include <stdio.h> #include <stdio.h>
void swap(int a, int b) { #define SWAP(x, y, T) do { T SWAP = x; x = y; y = SWAP; } while (0)
int t = a;
a = b;
b = t;
}
void gnome_sort(int array[], int n) { void gnome_sort(int array[], int n) {
int i = 0; int i = 0;
while (i < n) { while (i < n) {
if (array[i] > array[i+1]) { if (array[i] > array[i+1]) {
//swap(array[i+1], array[i]); SWAP(array[i], array[i+1], int);
int t = array[i];
array[i] = array[i+1];
array[i+1] = t;
if (i > 0) i--; if (i > 0) i--;
} }
else i++; else i++;