# Shuffle a deck of cards

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. ```

## C++

 `// C++ program for shuffling desk of cards.` `#include ` `using` `namespace` `std;`   `// Function which shuffle and print the array` `void` `shuffle(``int` `card[], ``int` `n)` `{` `    ``// Initialize seed randomly` `    ``srand``(``time``(0));`   `    ``for` `(``int` `i=0; i

## Java

 `// Java Code for Shuffle a deck of cards` `import` `java.util.Random;`   `class` `GFG {` `    `  `    ``// Function which shuffle and print the array` `    ``public` `static` `void` `shuffle(``int` `card[], ``int` `n)` `    ``{` `        `  `        ``Random rand = ``new` `Random();` `        `  `        ``for` `(``int` `i = ``0``; i < n; i++)` `        ``{` `            ``// Random for remaining positions.` `            ``int` `r = i + rand.nextInt(``52` `- i);` `            `  `             ``//swapping the elements` `             ``int` `temp = card[r];` `             ``card[r] = card[i];` `             ``card[i] = temp;` `             `  `        ``}` `    ``}` `     `  `    ``// Driver code` `    ``public` `static` `void` `main(String[] args) ` `    ``{` `        ``// Array from 0 to 51` `        ``int` `a[] = {``0``, ``1``, ``2``, ``3``, ``4``, ``5``, ``6``, ``7``, ``8``,` `                   ``9``, ``10``, ``11``, ``12``, ``13``, ``14``, ``15``,` `                   ``16``, ``17``, ``18``, ``19``, ``20``, ``21``, ``22``,` `                   ``23``, ``24``, ``25``, ``26``, ``27``, ``28``, ``29``,` `                   ``30``, ``31``, ``32``, ``33``, ``34``, ``35``, ``36``,` `                   ``37``, ``38``, ``39``, ``40``, ``41``, ``42``, ``43``,` `                   ``44``, ``45``, ``46``, ``47``, ``48``, ``49``, ``50``, ` `                   ``51``};` `     `  `        ``shuffle(a, ``52``);` `     `  `        ``// Printing all shuffled elements of cards` `        ``for` `(``int` `i = ``0``; i < ``52``; i ++)` `           ``System.out.print(a[i]+``" "``);` `        `  `    ``}` `}` `// This code is contributed by Arnav Kr. Mandal`

## Python3

 `# Python3 program for shuffling desk of cards` `# Function which shuffle and print the array `   `import` `random` `def` `shuffle(card,n) :` `    `  `    ``# Initialize seed randomly` `    ``for` `i ``in` `range``(n):` `        `  `        ``# Random for remaining positions.` `        ``r ``=` `i ``+` `(random.randint(``0``,``55``) ``%` `(``52` `-``i))` `        ``tmp``=``card[i]` `        ``card[i]``=``card[r]` `        ``card[r]``=``tmp` `#Driver code` `if` `__name__``=``=``'__main__'``:` `    ``a``=``[``0``, ``1``, ``2``, ``3``, ``4``, ``5``, ``6``, ``7``, ``8``,` `       ``9``, ``10``, ``11``, ``12``, ``13``, ``14``, ``15``,` `       ``16``, ``17``, ``18``, ``19``, ``20``, ``21``, ``22``, ` `       ``23``, ``24``, ``25``, ``26``, ``27``, ``28``, ``29``,` `       ``30``, ``31``, ``32``, ``33``, ``34``, ``35``, ``36``,` `       ``37``, ``38``, ``39``, ``40``, ``41``, ``42``, ``43``, ` `       ``44``, ``45``, ``46``, ``47``, ``48``, ``49``, ``50``,` `       ``51``]` `    ``shuffle(a,``52``)` `    ``print``(a)` `       `  `#this code is contributed by sahilshelangia`

## C#

 `// C# Code for Shuffle a deck of cards` `using` `System;`   `class` `GFG {` `    `  `    ``// Function which shuffle and ` `    ``// print the array` `    ``public` `static` `void` `shuffle(``int` `[]card, ` `                               ``int` `n)` `    ``{` `        `  `        ``Random rand = ``new` `Random();` `        `  `        ``for` `(``int` `i = 0; i < n; i++)` `        ``{` `            `  `            ``// Random for remaining positions.` `            ``int` `r = i + rand.Next(52 - i);` `            `  `            ``//swapping the elements` `            ``int` `temp = card[r];` `            ``card[r] = card[i];` `            ``card[i] = temp;` `            `  `        ``}` `    ``}` `    `  `    ``// Driver code` `    ``public` `static` `void` `Main() ` `    ``{` `        ``// Array from 0 to 51` `        ``int` `[]a = {0, 1, 2, 3, 4, 5, 6, 7, 8,` `                   ``9, 10, 11, 12, 13, 14, 15,` `                   ``16, 17, 18, 19, 20, 21, 22,` `                   ``23, 24, 25, 26, 27, 28, 29,` `                   ``30, 31, 32, 33, 34, 35, 36,` `                   ``37, 38, 39, 40, 41, 42, 43,` `                   ``44, 45, 46, 47, 48, 49, 50, ` `                   ``51};` `    `  `        ``shuffle(a, 52);` `    `  `        ``// Printing all shuffled elements of cards` `        ``for` `(``int` `i = 0; i < 52; i ++)` `        ``Console.Write(a[i]+``" "``);` `        `  `    ``}` `}`   `// This code is contributed by Nitin Mittal.`

## Javascript

 `// JavaScript Implementation of the above approach`   `// Function which shuffle and print the array ` `function` `shuffle(card, n) {` `  ``// Initialize seed randomly` `  ``for` `(let i = 0; i < n; i++) {` `    ``// Random for remaining positions.` `    ``let r = i + (Math.floor(Math.random() * (52 - i)));` `    ``let tmp = card[i];` `    ``card[i] = card[r];` `    ``card[r] = tmp;` `  ``}` `}`   `// Driver code` `  ``let a = [` `    ``0,` `    ``1,` `    ``2,` `    ``3,` `    ``4,` `    ``5,` `    ``6,` `    ``7,` `    ``8,` `    ``9,` `    ``10,` `    ``11,` `    ``12,` `    ``13,` `    ``14,` `    ``15,` `    ``16,` `    ``17,` `    ``18,` `    ``19,` `    ``20,` `    ``21,` `    ``22,` `    ``23,` `    ``24,` `    ``25,` `    ``26,` `    ``27,` `    ``28,` `    ``29,` `    ``30,` `    ``31,` `    ``32,` `    ``33,` `    ``34,` `    ``35,` `    ``36,` `    ``37,` `    ``38,` `    ``39,` `    ``40,` `    ``41,` `    ``42,` `    ``43,` `    ``44,` `    ``45,` `    ``46,` `    ``47,` `    ``48,` `    ``49,` `    ``50,` `    ``51,` `  ``];` `  ``shuffle(a, 52);` `  ``console.log(a);`

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.

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.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.

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next