1 / 11

Shuffle an Array

Shuffle an Array. Shuffle a deck of cards. In i th iteration, choose a random element from remainder of deck and put at index i. choose random integer r between i and N-1 swap values in positions r and i. Array index. 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. Value. 9 . 2 . 3 . 4 .

Download Presentation

Shuffle an Array

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Shuffle an Array • Shuffle a deck of cards. • In ith iteration, choose a random element from remainder of deck and put at index i. • choose random integer r between i and N-1 • swap values in positions r and i Array index 0 1 2 3 4 5 6 7 8 9 Value 9 2 3 4 5 6 7 8 9 2 10 J random integer = 7

  2. Shuffle an Array • Shuffle a deck of cards. • In ith iteration, choose a random element from remainder of deck and put at index i. • choose random integer r between i and N-1 • swap values in positions r and i Array index 0 1 2 3 4 5 6 7 8 9 Value 9 3 5 4 5 3 6 7 8 2 10 J random integer = 3

  3. Shuffle an Array • Shuffle a deck of cards. • In ith iteration, choose a random element from remainder of deck and put at index i. • choose random integer r between i and N-1 • swap values in positions r and i Array index 0 1 2 3 4 5 6 7 8 9 Value 9 5 4 J 3 6 7 8 2 10 J 4 random integer = 9

  4. Shuffle an Array • Shuffle a deck of cards. • In ith iteration, choose a random element from remainder of deck and put at index i. • choose random integer r between i and N-1 • swap values in positions r and i Array index 0 1 2 3 4 5 6 7 8 9 Value 9 5 J 3 4 6 7 8 2 10 4 3 random integer = 9

  5. Shuffle an Array • Shuffle a deck of cards. • In ith iteration, choose a random element from remainder of deck and put at index i. • choose random integer r between i and N-1 • swap values in positions r and i Array index 0 1 2 3 4 5 6 7 8 9 Value 9 5 J 4 6 8 7 8 6 2 10 3 random integer = 6

  6. Shuffle an Array • Shuffle a deck of cards. • In ith iteration, choose a random element from remainder of deck and put at index i. • choose random integer r between i and N-1 • swap values in positions r and i Array index 0 1 2 3 4 5 6 7 8 9 Value 9 5 J 4 8 7 3 6 2 10 3 7 random integer = 9

  7. Shuffle an Array • Shuffle a deck of cards. • In ith iteration, choose a random element from remainder of deck and put at index i. • choose random integer r between i and N-1 • swap values in positions r and i Array index 0 1 2 3 4 5 6 7 8 9 Value 9 5 J 4 8 3 6 10 2 10 6 7 random integer = 8

  8. Shuffle an Array • Shuffle a deck of cards. • In ith iteration, choose a random element from remainder of deck and put at index i. • choose random integer r between i and N-1 • swap values in positions r and i Array index 0 1 2 3 4 5 6 7 8 9 Value 9 5 J 4 8 3 10 2 7 6 7 2 random integer = 9

  9. Shuffle an Array • Shuffle a deck of cards. • In ith iteration, choose a random element from remainder of deck and put at index i. • choose random integer r between i and N-1 • swap values in positions r and i Array index 0 1 2 3 4 5 6 7 8 9 Value 9 5 J 4 8 3 10 7 6 6 2 random integer = 8

  10. Shuffle an Array • Shuffle a deck of cards. • In ith iteration, choose a random element from remainder of deck and put at index i. • choose random integer r between i and N-1 • swap values in positions r and i Array index 0 1 2 3 4 5 6 7 8 9 Value 9 5 J 4 8 3 10 7 6 2 2 random integer = 9

  11. Shuffle an Array • Shuffle a deck of cards. • In ith iteration, choose a random element from remainder of deck and put at index i. • choose random integer r between i and N-1 • swap values in positions r and i Array index 0 1 2 3 4 5 6 7 8 9 Value 9 5 J 4 8 3 10 7 6 2

More Related