This blog post explores the Lehmer code, a way of mapping integers to permutations. It can be used to compute a random permutation (by computing a random integer and mapping it to a permutation) and more. Permutations A permutation of an array is an array that contains the same elements, but possibly in a different order. For example, given the array [ 'a', 'b', 'c' ] All of its permutations are: [ 'a', 'b', 'c' ] [ 'a', 'c', 'b' ] [ 'b', 'a', 'c' ] [ 'b', 'c', 'a' ] [ 'c', 'a', 'b' ] [ 'c', 'b', 'a' ] Computing a permutation: a naive solution In order to motivate the Lehmer code, let’s first implement a naive algorithm for computing a permutation of an array. Given the following array arr . var arr = [ 'a', 'b', 'c' ]; A simple way of computing a random permutation...
Comments
Post a Comment