programming-examples/java/Numerical_Problems/Java Program to Implement Fisher-Yates Algorithm for Array Shuffling.java
2019-11-15 12:59:38 +01:00

60 lines
1.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
This is java implementation of the FisherYates shuffle (named after Ronald Fisher and Frank Yates)algorithm, also known as the Knuth shuffle (after Donald Knuth), for generating a random permutation of a finite set—in plain terms, for randomly shuffling the set.
*/
//This is a sample program to shuffle the elements of an array using Fisher Yates Array Shuffling algorithm
import java.util.Random;
import java.util.Scanner;
public class Fisher_Yates_Array_Shuffling
{
static int[] fisherYatesShuffling(int []arr, int n)
{
int []a = new int[n];
int []ind = new int[n];
for(int i=0; i<n; i++)
ind[i] = 0;
int index;
Random rand = new Random();
for(int i=0; i<n; i++)
{
do
{
index = rand.nextInt(n);
}
while(ind[index] != 0);
ind[index] = 1;
a[i] = arr[index];
}
return a;
}
public static void main(String agrs[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array size: ");
int n = sc.nextInt();
System.out.println("Enter the array elements: ");
int []a = new int[n];
int []res = new int[n];
for(int i=0; i<n; i++)
{
a[i] = sc.nextInt();
}
res = fisherYatesShuffling(a, n);
for(int i=0; i<n; i++)
{
System.out.print(res[i]+" ");
}
sc.close();
}
}
/*
Enter the array size:
12
Enter the array elements:
1 2 3 4 5 6 7 8 9 10 11 12
The shuffled elements are:
7 10 8 4 9 6 12 3 2 1 5 11