Given a deck of cards, the task is to shuffle them.
Asked in Amazon Interview
Prerequisite : Shuffle a given array
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.
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
Note : Output will be different each time because of the random function used in the program.
Please refer Shuffle a given array for details.
This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Shuffle a given array using Fisher–Yates shuffle Algorithm
- Find the player who wins the game by removing the last of given N cards
- Shuffle or Randomize a list in Java
- Find N random points within a Circle
- Random Numbers Ecosystem in Julia - The Natural Side
- Slow Start Backoff Algorithm for Ad-Hoc
- Construct a matrix such that union of ith row and ith column contains every element from 1 to 2N-1
- Maximum String Partition
- Maximum length sub-array which satisfies the given conditions
- Generating Random String Using PHP
- Generating OTP (One time Password) in PHP
- Mid-Square hashing
- Strong Password Suggester Program
- Freivald’s Algorithm to check if a matrix is product of two