/* * http://forum.codecall.net/topic/64715-how-to-generate-all-permutations/ * * Generation of all possible permutations of an array's elements is * a problem that can be done naturally with recursion. * * Let's take a closeur look, given a string of 3 elements i.e. {'x', 'y', 'z'}. * How did you go about printing all permutations on paper? * * Here is how we went: * * x followed by all permutations of y and z (yz and zy) * y followed by all permutations of x and z (xz and zx) * z followed by all permutations of x and y (xy and yx) * * So to break down the problem, we fix one element in the initial * position and try to change the rest of them for all possible * situations. To achieve this, we call the same function recursively * again, with one less element since the first one's possibilities are * already figured out. * * To do this in code, we have a function called permutation. It takes * two understandable arguments, an array and its length * * However, there is one more parameter that we will need. Because we * have to move ahead in the array with every call i.e. first we fix * first element, then second and so on. So this basically means we are * going to increase the starting index of array. * * Now we are passing a character array, so each time we have to add * another parameter called starting or current index of the permutations * to be explored in the current call. * * If this index is 0, for the example above it means x is fixed and we * generate further permutations of y and z, when this is 1, it means * first two places are fixed and it is the third and onwards positions * that need to be moved. * * Next we get inside our actual function. It has two main parts. * * The first part - the base case - prints a permutation when it is ready. * * The second part is the meat of the function. It runs a loop from our * current index to size of array. In each iteration of that loop it * swaps the current index and loop variable. Right at that point it * calls the permutation function again recursively to generate all * further permutations of this swapped state. Once that call is done, it * swaps back the variables to original state. This is required because * for e.g. after we are done with generating for x (yz and zy) we want * to start from the original state again i.e. original state was xyz so * now we move y to the first place by swapping it with x, so y followed * by xz and then zx would be the sequence. * */ void use_permutation(int *arr, int size); void swap(int *fir, int *sec) { int temp = *fir; *fir = *sec; *sec = temp; } /* * arr is the array, curr is the current index to start * permutation from, size is sizeof the arr */ void permutation(int *arr, int curr, int size) { int i; if(curr == size - 1) { // a permutation is ready, use it ... use_permutation(arr, size); } else { for(i = curr; i < size; i++) { swap(&arr[curr], &arr[i]); permutation(arr, curr+1, size); swap(&arr[curr], &arr[i]); } } } int main(void) { int str[] = {0, 1, 2, 3, 4, 5, 6, 7}; permutation(str, 0, sizeof(str)/sizeof(*str)); return 0; }