Created
December 4, 2013 10:18
-
-
Save antirez/7785318 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <sys/types.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <time.h> | |
void combsort(long long *a, size_t l) { | |
size_t j; | |
size_t gap = l > 1 ? l / 2 : 1; | |
if (l == 0) return; | |
while(1) { | |
int swapped = 0; | |
for (j = 0; j < l-gap; j++) { | |
printf("."); | |
if (a[j] < a[j+gap]) { | |
long long aux = a[j]; | |
a[j] = a[j+gap]; | |
a[j+gap] = aux; | |
swapped = 1; | |
} | |
} | |
if (gap > 1) | |
gap = gap * 3 / 4; /* This is like / 1.333... */ | |
else if (swapped == 0) | |
break; /* Array is sorted. */ | |
} | |
} | |
int main(void) { | |
long long offsets[10]; | |
int j; | |
srand(time(NULL)^getpid()); | |
for (j = 0; j < 10; j++) | |
offsets[j] = rand(); | |
combsort(offsets,10); | |
printf("\n"); | |
for (j = 0; j < 10; j++) | |
printf("%lld\n", offsets[j]); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment