title: Efficiently calculate next permutation of length k from n choices
body: <p>I need to <strong>efficiently</strong> calculate the next permutation of length <code>k</code> from <code>n</code>
  choices. Wikipedia lists <a href="http://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order" rel="noreferrer">a great
  algorithm</a>
  for computing the next permutation of length <code>n</code> from <code>n</code> choices.</p>
  
  <p>The best thing I can come up with is using that algorithm (or <a href="http://en.wikipedia.org/wiki/Steinhaus%E2%80%93Johnson%E2%80%93Trotter_algorithm" rel="noreferrer">the Steinhaus–Johnson–Trotter algorithm</a>), and then just only considering the first <code>k</code> items of the list, and iterating again whenever the changes are all above that position.</p>
  
  <p>Constraints:</p>
  
  <ul>
  <li>The algorithm must calculate the next permutation given nothing more than
  the current permutation. If it needs to generate a list of all permutations,
  it will take up too much memory.</li>
  <li>It must be able to compute a permutation of only length <code>k</code> of <code>n</code> (this is
  where the other algorithm fails</li>
  </ul>
  
  <p>Non-constraints:</p>
  
  <ul>
  <li>Don't care if it's in-place or not</li>
  <li>I don't care if it's in lexographical order, or any order for that matter</li>
  <li>I don't care <em>too much</em> how efficiently it computes the next permutation,
  within reason of course, it can't give me the next permutation by making a
  list of all possible ones each time.</li>
  </ul>