Randomly sorting an array

Posted by Cam on Stack Overflow See other posts from Stack Overflow or by Cam
Published on 2011-01-01T11:30:44Z Indexed on 2011/01/01 11:54 UTC
Read the original article Hit count: 207

Filed under:
|
|

Does there exist an algorithm which, given an ordered list of symbols {a1, a2, a3, ..., ak}, produces in O(n) time a new list of the same symbols in a random order without bias? "Without bias" means the probability that any symbol s will end up in some position p in the list is 1/k.

Assume it is possible to generate a non-biased integer from 1-k inclusive in O(1) time. Also assume that O(1) element access/mutation is possible, and that it is possible to create a new list of size k in O(k) time.

In particular, I would be interested in a 'generative' algorithm. That is, I would be interested in an algorithm that has O(1) initial overhead, and then produces a new element for each slot in the list, taking O(1) time per slot.

If no solution exists to the problem as described, I would still like to know about solutions that do not meet my constraints in one or more of the following ways (and/or in other ways if necessary):

  • the time complexity is worse than O(n).
  • the algorithm is biased with regards to the final positions of the symbols.
  • the algorithm is not generative.

I should add that this problem appears to be the same as the problem of randomly sorting the integers from 1-k, since we can sort the list of integers from 1-k and then for each integer i in the new list, we can produce the symbol ai.

© Stack Overflow or respective owner

Related posts about language-agnostic

Related posts about sorting