Skip to content

Instantly share code, notes, and snippets.

@cocoatomo
Created June 17, 2010 16:43
Show Gist options
  • Save cocoatomo/442377 to your computer and use it in GitHub Desktop.
Save cocoatomo/442377 to your computer and use it in GitHub Desktop.
#include "permut-1.0.h"
#include "fact.h"
void swap(int *sequence, int i, int j) {
int tmp;
tmp = sequence[i];
sequence[i] = sequence[j];
sequence[j] = tmp;
}
void permut(int *sequence, size_t size, unsigned int index) {
int pos;
int factorial = fact(--size);
do {
pos = index / factorial;
index %= factorial;
factorial /= size;
swap(sequence, pos, size);
} while (--size);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment