Use swap macro
This commit is contained in:
parent
d78469d21a
commit
abf965e7ce
1 changed files with 2 additions and 9 deletions
11
gnome-sort.c
11
gnome-sort.c
|
|
@ -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++;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue