Bad algorithm
Date: 06/07/06
(Algorithms) Keywords: no keywords
I think it's sad when educational institutions have errors in their pages. Here is one on a SparkNotes page.
(The below code is not mine, it is copied from the above page.)
void print_permutations(int arr[], int n, int i)
{
int j, swap;
print_array(arr,n);
for (j=i+1; j
Now, there is an error in the for loop where the j should be j++, but even more than that, the purpose of this function (supposedly) is to print all permutations of a data set. I traced it on paper and got 16 (instead of 24,the intended number) , so I figured I'd traced it wrong, and double-checked my work. Then I ran it in a compiler where I in fact got 16 results.
Now, I'm curious if I'm misunderstanding something very fundamental. Does "Let's write a function to take an array of integers and print out every permutation of it" have some meaning different than I think it does? Or is this "study guide" page really just spouting misinformation? Any idea on what the code is really supposed to look like?
Source: http://community.livejournal.com/algorithms/76631.html