C++ Program to Implement Fisher-Yates Algorithm

«
»
This is a C++ Program to shuffle array using Fisher-Yates algorithm. The Fisher–Yates shuffle (named after Ronald Fisher and Frank Yates), also known as the Knuth shuffle (after Donald Knuth), is an algorithm for generating a random permutation of a finite set—in plain terms, for randomly shuffling the set. A variant of the Fisher–Yates shuffle, known as Sattolo’s algorithm, may be used to generate random cycles of length n instead. The Fisher–Yates shuffle is unbiased, so that every permutation is equally likely. The modern version of the algorithm is also rather efficient, requiring only time proportional to the number of items being shuffled and no additional storage space.

Here is source code of the C++ Program to Implement Fisher-Yates Algorithm for Array Shuffling. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <iostream>
  2. #include <stdlib.h>
  3.  
  4. using namespace std;
  5.  
  6. void fisherYatesShuffling(int *arr, int n)
  7. {
  8.     int a[n];
  9.     int ind[n];
  10.     for (int i = 0; i < n; i++)
  11.         ind[i] = 0;
  12.     int index;
  13.  
  14.     for (int i = 0; i < n; i++)
  15.     {
  16.         do
  17.         {
  18.             index = rand() % n;
  19.         }
  20.         while (ind[index] != 0);
  21.  
  22.         ind[index] = 1;
  23.         a[i] = *(arr + index);
  24.     }
  25.     for (int i = 0; i < n; i++)
  26.     {
  27.         cout << a[i] << " ";
  28.     }
  29. }
  30.  
  31. int main(int argc, char **argv)
  32. {
  33.     cout << "Enter the array size: ";
  34.     int n;
  35.     cin >> n;
  36.     cout << "Enter the array elements: ";
  37.     int a[n];
  38.     for (int i = 0; i < n; i++)
  39.     {
  40.         cin >> a[i];
  41.     }
  42.     fisherYatesShuffling(a, n);
  43. }

Output:

advertisement
$ g++ Fisher-YatesShuffling.cpp
$ a.out
 
Enter the array size: 7
Enter the array elements: 12 23 34 45 56 67 78
78 23 67 45 34 12 56

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Here’s the list of Best Books in C++ Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.