Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Does Fannkuch Work?

Tags:

algorithm

I'm having trouble understanding the instructions of implementing Fannkuch. Instructions: http://www.haskell.org/haskellwiki/Shootout/Fannkuch

After step "Count the number of flips, here 5.", I am lost.

like image 781
mudge Avatar asked Mar 04 '10 04:03

mudge


1 Answers

Wow, yes, that is not the greatest algorithm description :).

My interpretation is they want you to do the following:

fannkuch(n) {
 int maxFlips = 0, printCount = 0;
 foreach permutation p of [1..n] {
  maxFlips = max(maxFlips, flipCount(p));
  if (printCount++ &lt 30) printPermutation(p);
 }
 print(maxFlips);
}

flipCount(p) {
 int count = 0;
 while (p[0] != 1) {
  reverse(p, p + p[0]);
  count++;
 }
 return count;
}
like image 157
Gary Linscott Avatar answered Nov 19 '22 23:11

Gary Linscott