Java Program to Implement Fisher-Yates Algorithm for Array Shuffling

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:

Create a Custom Exception in Java
Spring Boot Configuration with Jasypt
Tạo chương trình Java đầu tiên sử dụng Eclipse
Shuffling Collections In Java
A Quick Guide to Using Keycloak with Spring Boot
Java Program to Check Whether Topological Sorting can be Performed in a Graph
Ignore Null Fields with Jackson
Debugging Reactive Streams in Java
Java Program to Test Using DFS Whether a Directed Graph is Strongly Connected or Not
Java Program to Implement Branch and Bound Method to Perform a Combinatorial Search
A Guide to the Java LinkedList
Java InputStream to String
Java Program to implement Dynamic Array
Java Program to Implement Direct Addressing Tables
What is Thread-Safety and How to Achieve it?
Returning Image/Media Data with Spring MVC
Java Program to Generate Random Hexadecimal Byte
Java Program to Implement Hash Tables Chaining with List Heads
Working With Maps Using Streams
Java Program to Implement LinkedList API
Concurrent Test Execution in Spring 5
Java Program to Perform Naive String Matching
Java Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences
Từ khóa throw và throws trong Java
Multipart Upload with HttpClient 4
Testing an OAuth Secured API with Spring MVC
Base64 encoding và decoding trong Java 8
Using JWT with Spring Security OAuth (legacy stack)
Creating a Custom Starter with Spring Boot
Java Program to find the maximum subarray sum using Binary Search approach
Composition, Aggregation, and Association in Java
Convert a Map to an Array, List or Set in Java