This is java implementation of the Fisher–Yates 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.
Here is the source code of the Java Program to Implement Fisher-Yates Algorithm for Array Shuffling. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
//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();
}
}
Output:
$ javac Fisher_Yates_Array_Shuffling.java $ java Fisher_Yates_Array_Shuffling 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
Related posts:
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
Java Program to Check Cycle in a Graph using Graph traversal
Java Program to Find kth Largest Element in a Sequence
So sánh ArrayList và Vector trong Java
Semaphore trong Java
Add Multiple Items to an Java ArrayList
Hướng dẫn sử dụng Java Annotation
String Joiner trong Java 8
Guide to UUID in Java
Java Program to Check whether Directed Graph is Connected using BFS
Java Program to Implement Fermat Primality Test Algorithm
Injecting Prototype Beans into a Singleton Instance in Spring
Check if a String is a Palindrome in Java
Convert a Map to an Array, List or Set in Java
Java Program to Implement Interpolation Search Algorithm
Java Program to Implement Double Ended Queue
Java Program to Implement Min Hash
Comparing Dates in Java
Java Program to Implement Solovay Strassen Primality Test Algorithm
Java Program to Implement Johnson’s Algorithm
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
Send an email using the SMTP protocol
Compare Two JSON Objects with Jackson
Flattening Nested Collections in Java
Using a Mutex Object in Java
Logging in Spring Boot
Wiring in Spring: @Autowired, @Resource and @Inject
Apache Camel with Spring Boot
Java Program to Perform Addition Operation Using Bitwise Operators
Batch Processing with Spring Cloud Data Flow
Introduction to Thread Pools in Java
Java Program to Find Transitive Closure of a Graph