Skip to content

Instantly share code, notes, and snippets.

@wpcarro
Created August 9, 2019 16:00
Show Gist options
  • Select an option

  • Save wpcarro/9cbc0c331969919d4040024a27c3c09f to your computer and use it in GitHub Desktop.

Select an option

Save wpcarro/9cbc0c331969919d4040024a27c3c09f to your computer and use it in GitHub Desktop.
Programming Pearls - disk sort
#!/usr/bin/env python3
import random
sample_size = 9000000
numbers = list(range(0, 10000000))
random.shuffle(numbers)
with open('numbers.txt', 'w') as f:
for i in numbers[0:sample_size]:
print('{}'.format(i))
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main(int argc, char *argv[]) {
FILE *file = fopen("numbers.txt", "r");
FILE *out = fopen("sorted.txt", "w");
char line[256];
int number;
char vb[10000000 / 8];
char current;
char mask;
int k;
while (fgets(line, sizeof(line), file)) {
number = atoi(line);
k = (int)floor(number / 8);
current = vb[k];
mask = 1 << (8 - (number % 8));
vb[k] = current | mask;
}
for (int i = 0; i < 10000000 / 8; i += 1) {
current = vb[i];
for (int j = 1; j < 8; j += 1) {
mask = 1 << (8 - j);
if (current & (!current | mask) != 0) {
fprintf(out, "%d\n", i * 8 + j);
}
}
}
fclose(file);
fclose(out);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment