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:
Registration – Activate a New Account by Email
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Java Program for Topological Sorting in Graphs
Java Program to Implement Bresenham Line Algorithm
Test a REST API with Java
Java Program to Implement Hash Tables with Linear Probing
Introduction to the Java NIO2 File API
Java Program to Generate Randomized Sequence of Given Range of Numbers
Enum trong java
Hướng dẫn Java Design Pattern – Strategy
Spring Boot - Logging
Java Program to Implement Queue using Two Stacks
Entity To DTO Conversion for a Spring REST API
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
Command-Line Arguments in Java
Tổng quan về ngôn ngữ lập trình java
Java Program to Check Whether Graph is DAG
Guide to java.util.concurrent.Locks
A Guide to the ResourceBundle
Bootstrapping Hibernate 5 with Spring
Jackson Exceptions – Problems and Solutions
Adding Shutdown Hooks for JVM Applications
Một số tính năng mới về xử lý ngoại lệ trong Java 7
Guide to BufferedReader
StringBuilder vs StringBuffer in Java
A Guide to @RepeatedTest in Junit 5
Serialize Only Fields that meet a Custom Criteria with Jackson
Java Program to Implement Queue using Linked List
Spring Cloud – Adding Angular
Java Program to Implement the Program Used in grep/egrep/fgrep
Java Program to Search for an Element in a Binary Search Tree
Java Program to Implement Ternary Tree