VOOZH about

URL: https://www.geeksforgeeks.org/dsa/shuffle-a-deck-of-cards-3/

⇱ Shuffle a deck of cards - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Shuffle a deck of cards

Last Updated : 23 Jul, 2025

Given a deck of cards, the task is to shuffle them. Asked in Amazon Interview

Prerequisite : Shuffle a given array 

Algorithm:

1. First, fill the array with the values in order.
2. Go through the array and exchange each element 
 with the randomly chosen element in the range 
 from itself to the end.

// It is possible that an element will be swap
// with itself, but there is no problem with that. 

Output:

29 27 20 23 26 21 35 51 15 18 46 32 33 19 
24 30 3 45 40 34 16 11 36 50 17 10 7 5 4 
39 6 47 38 28 13 44 49 1 8 42 43 48 0 12 
37 41 25 2 31 14 22

Time Complexity: O(n)
Space Complexity: O(1)

Note : Output will be different each time because of the random function used in the program. Please refer Shuffle a given array for details. 

Comment
Article Tags:
Article Tags: