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:
An Intro to Spring Cloud Zookeeper
CharSequence vs. String in Java
Java Program to Implement a Binary Search Tree using Linked Lists
Java Program to Implement D-ary-Heap
Java Program to Perform Quick Sort on Large Number of Elements
Spring Boot - Hystrix
Phương thức tham chiếu trong Java 8 – Method References
Extract links from an HTML page
Java Program to Implement Hash Tree
Sorting Query Results with Spring Data
New Stream Collectors in Java 9
Giới thiệu Design Patterns
Java Program to Implement Ford–Fulkerson Algorithm
Chương trình Java đầu tiên
OAuth2.0 and Dynamic Client Registration
Tránh lỗi NullPointerException trong Java như thế nào?
Java Program to Solve the 0-1 Knapsack Problem
Custom Cascading in Spring Data MongoDB
Java Program to Implement Knapsack Algorithm
Java Program to Implement Threaded Binary Tree
Spring Boot - Tracing Micro Service Logs
Spring MVC Setup with Kotlin
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Giới thiệu HATEOAS
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Java Program to Perform Postorder Non-Recursive Traversal of a Given Binary Tree
Java Program to Implement Nth Root Algorithm
The Registration API becomes RESTful
Tính kế thừa (Inheritance) trong java
Java Program to Implement Sorted Vector
Spring Boot - Apache Kafka
Java Program to Find the Edge Connectivity of a Graph