Skip to content

Instantly share code, notes, and snippets.

@antirez
Created December 4, 2013 10:18
Show Gist options
  • Save antirez/7785318 to your computer and use it in GitHub Desktop.
Save antirez/7785318 to your computer and use it in GitHub Desktop.
#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