
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Implement Fisher-Yates Algorithm for Array Shuffling
Fisher-Yates algorithm generates a random permutation of the array elements i.e. it randomly shuffles all the elements of an array. All the permutations for the array are equally likely as the Fisher-Yates algorithm is unbiased.
A program to implement the Fisher-Yates Algorithm for array shuffling in C++ is given as follows −
Example
#include <iostream> #include <t;stdlib.h> using namespace std; int main() { int n; cout << "Enter the array size: "<<endl; cin >> n; int arr[n], arr1[n], index_arr[n]; int index; cout << "Enter the array elements: "<<endl; for (int i = 0; i < n; i++) cin >> arr[i]; for (int i = 0; i < n; i++) index_arr[i] = 0; for (int i = 0; i < n; i++) { do { index = rand() % n; } while (index_arr[index] != 0); index_arr[index] = 1; arr1[i] = arr[index]; } cout<<"The shuffled array is: "; for (int i = 0; i < n; i++) cout << arr1[i] << " "; return 0; }
Output
The output of the above program is as follows
Enter the array size: 10 Enter the array elements: 1 2 3 4 5 6 7 8 9 10 The shuffled array is: 4 7 8 6 3 10 2 1 9 5
In the above program, the size of the array and the array are requested from the user. This is given below −
cout << "Enter the array size: "<<endl; cin >> n; int arr[n], arr1[n], index_arr[n]; int index; cout << "Enter the array elements: "<<endl; for (int i = 0; i < n; i++) cin >> arr[i];
After the array is obtained, index_arr[] is initialized to 0. Then the rand() function is used to randomly store the values of arr[] into arr1[]. This is demonstrated by the following code snippet −
for (int i = 0; i < n; i++) { do { index = rand() % n; } while (index_arr[index] != 0); index_arr[index] = 1; arr1[i] = arr[index]; }
Finally the shuffled array is displayed. This is seen below −
cout<<"The shuffled array is: "; for (int i = 0; i < n; i++) cout << arr1[i] << " ";
- Related Articles
- C++ Program to Implement Wagner and Fisher Algorithm for online String Matching
- What is Fisher–Yates shuffle in JavaScript?
- C++ Program to Implement Bitap Algorithm for String Matching
- C++ Program to Implement Kadane’s Algorithm
- C++ Program to Implement Johnson’s Algorithm
- C++ Program to Implement the RSA Algorithm
- C++ Program to Implement Coppersmith Freivald’s Algorithm
- C++ Program to Implement Modular Exponentiation Algorithm
- C++ Program to Implement Nearest Neighbour Algorithm
- C++ Program to Implement Expression Tree Algorithm
- C++ Program to Implement Interpolation Search Algorithm
- C++ Program to Implement Extended Euclidean Algorithm
- C program to implement Euclid’ s algorithm
- C Program for Reversal algorithm for array rotation
- Java Program for Reversal algorithm for array rotation

Advertisements