-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday26.cpp
More file actions
34 lines (30 loc) · 765 Bytes
/
day26.cpp
File metadata and controls
34 lines (30 loc) · 765 Bytes
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
#include<bits/stdc++.h>
using namespace std;
// Shuffle a given array of elements (Fisher–Yates shuffle)
// Problem Statement
/*
Given an array of integers, in-place shuffle it.
The algorithm should produce an unbiased permutation
i.e. every permutation is equally likely. */
void swapRandom(vector<int>&arr,int n){
for(int i=n-1;i>=1;i--){
// Generate a random number j such that 0 <= j <= i
int j = rand()%(i+1);
swap(arr[i],arr[j]);
}
}
main(){
int n,i,j;
cin>>n;
vector<int>arr(n);
for(i=0;i<n;i++){
cin>>arr[i];
}
srand(time(NULL));
swapRandom(arr,n);
//Print shuffled array
for(i=0;i<n;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}