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 firstname.lastname@example.org. 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.
- Shuffle a given array using Fisher–Yates shuffle Algorithm
- Shuffle or Randomize a list in Java
- Maximum String Partition
- Maximum length sub-array which satisfies the given conditions
- Generating Random String Using PHP
- Mid-Square hashing
- Freivald’s Algorithm to check if a matrix is product of two
- Select a Random Node from a tree with equal probability
- QuickSort using Random Pivoting
- Operations on Sparse Matrices
- Random Walk (Implementation in Python)
- Expectation or expected value of an array
- Estimating the value of Pi using Monte Carlo
- Binomial Random Variables