Fast Topswops calculation From AZSPCS:

Suppose you have a deck containing n cards. Each card contains a number from 1 to n, and each number appears on exactly one card. You look at the number on the top card -- let's says it's k -- and then reverse the order of the top k cards. You continue this procedure -- reading the top number and then reversing the corresponding number of cards -- until the top card is 1.

Write the fastest program to compute the number of reversals for a given deck. Note that if you are participating in the contest you are not allowed to post your code (and thus I will not post my code yet).
JavaScript

 

function(d){for(t=0;x=(n=d[0])-1;t++)for(i=0;i&lt;n/2;i++){m=d[x-i];d[x-i]=d[i];d[i]=m}return t}


You pass it the deck, like so:

f([3, 2, 1]) // 1
f([2, 3, 1]) // 2
f([1, 2, 3]) // 0