Code:
#include iostream
#include stdlib.h
using namespace std;
void fisherYatesShuffling(int *arr, int n)
{
int a[n];
int ind[n];
for (int i = 0; i < n; i++)
ind[i] = 0;
int index;
for (int i = 0; i < n; i++)
{
do
{
index = rand() % n;
}
while (ind[index] != 0);
ind[index] = 1;
a[i] = *(arr + index);
}
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
}
int main(int argc, char **argv)
{
cout << "Enter the array size: ";
int n;
cin >> n;
cout << "Enter the array elements: ";
int a[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
fisherYatesShuffling(a, n);
}
Output:
Enter the array size: 7
Enter the array elements: 12 23 34 45 56 67 78
78 23 67 45 34 12 56
More C++ Programs: