120 likes | 295 Views
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 .
E N D
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
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
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
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
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
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
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
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
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
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
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