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.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

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

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest 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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.