BIRT 3.7
Written by: Michael Williams
Featured Refcardz: Top Refcardz:
  1. Scrum
  2. Apache Maven 2
  3. Essential MySQL
  4. Node.js
  5. Groovy
  1. jQuery Selectors
  2. Ajax
  3. Java
  4. Spring Config.
  5. Java Concurrency

Link Details

Link 118012 thumbnail
User 111696 avatar

By bloid
via adrianquark.blogspot.com
Published: Sep 25 2008 / 16:06

Today at a career fair, an Amazon recruiter asked me how to shuffle an array. Sounds like an easy problem, right? It turns out a good majority of people posting code to the internet get it wrong. But before reading on, try it out yourself, see what you come up with, and then Google for "array shuffle" or "randomize array" to see what other people think.
  • 15
  • 0
  • 1981
  • 0

Comments

Add your comment
User 239201 avatar

amphi replied ago:

1 votes Vote down Vote up Reply

http://en.wikipedia.org/wiki/Fisher-Yates_shuffle

That algorithm basically puts everything into a bag and draws the items one by one. The cursor divides it into inside and outside the bag. If you think about it like that it should make more sense. Java's Collections.shuffle() for example utilizes this well-known algorithm.

A continuous variation of that algorithm (shuffles one item on-draw):
http://kaioa.com/node/53 (Never-ending Shuffled Sequences - When Random is too Random)

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.