Skip to content

Instantly share code, notes, and snippets.

@jotaki
Created October 3, 2013 03:21
Show Gist options
  • Save jotaki/6804342 to your computer and use it in GitHub Desktop.
Save jotaki/6804342 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void swap(int *p, int i, int j)
{
int t = p[i];
p[i] = p[j];
p[j] = t;
}
void generate(int n, int *seed, char **p, unsigned long *i)
{
int c;
if(n == 0) {
p[*i] = calloc(11, sizeof(char));
if(p[*i]) {
for(c = 0; c < 10; ++c) {
p[*i][c] = seed[c] + '0';
}
(*i)++;
}
return;
}
for(c = 0; c <= n; ++c) {
swap(seed, c, n);
generate(n - 1, seed, p, i);
swap(seed, c, n);
}
}
int vstrcmp(const void *a, const void *b)
{
return strcmp(* (char * const *)a, * (char * const *)b);
}
int main()
{
int seed[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
char **output = NULL;
unsigned long i = 0;
output = calloc(3628800, sizeof(char*));
if(output == NULL) {
perror("uh oh");
return 1;
}
generate(9, seed, output, &i);
qsort(output, i, sizeof(char*), vstrcmp);
do {
if(output[i]) {
printf("%s\n", output[i]);
free(output[i]);
}
} while(i-- != 0);
free(output);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment