Java Program to Implement Fisher-Yates Algorithm

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.

  1. //This is a sample program to shuffle the elements of an array using Fisher Yates Array Shuffling algorithm
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class Fisher_Yates_Array_Shuffling 
  6. {
  7.     static int[] fisherYatesShuffling(int []arr, int n)
  8.     {
  9.         int []a = new int[n];
  10.         int []ind = new int[n];
  11.         for(int i=0; i<n; i++)
  12.             ind[i] = 0;
  13.         int index;
  14.         Random rand = new Random();
  15.         for(int i=0; i<n; i++)
  16.         {
  17.             do
  18.             {
  19.                 index = rand.nextInt(n);
  20.             } while(ind[index] != 0);
  21.  
  22.             ind[index] = 1;
  23.             a[i] = arr[index];
  24.         }
  25.         return a;
  26.     }
  27.  
  28.     public static void main(String agrs[])
  29.     {
  30.         Scanner sc = new Scanner(System.in);
  31.         System.out.println("Enter the array size: ");
  32.         int n = sc.nextInt();
  33.         System.out.println("Enter the array elements: ");
  34.         int []a = new int[n];
  35.         int []res = new int[n];
  36.         for(int i=0; i<n; i++)
  37.         {
  38.             a[i] = sc.nextInt();
  39.         }
  40.         res = fisherYatesShuffling(a, n);
  41.         for(int i=0; i<n; i++)
  42.         {
  43.             System.out.print(res[i]+" ");
  44.         }
  45.         sc.close();
  46.     }
  47. }

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

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.