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:
Removing all Nulls from a List in Java
Migrating from JUnit 4 to JUnit 5
Java Program to Implement Bloom Filter
How to Find an Element in a List with Java
Upload and Display Excel Files with Spring MVC
Java Program to Implement Doubly Linked List
Chương trình Java đầu tiên
Number Formatting in Java
Java Program to Find Transitive Closure of a Graph
Java Program to Find the Vertex Connectivity of a Graph
ThreadPoolTaskExecutor corePoolSize vs. maxPoolSize
Java Program to Generate a Graph for a Given Fixed Degree Sequence
The Difference Between map() and flatMap()
Adding a Newline Character to a String in Java
Hướng dẫn tạo và sử dụng ThreadPool trong Java
Java Program to Implement Pagoda
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Introduction to Spring Security Expressions
A Guide to JUnit 5 Extensions
Spring Boot - Scheduling
HashSet trong Java hoạt động như thế nào?
Functional Interface trong Java 8
Comparing Arrays in Java
Hướng dẫn Java Design Pattern – Singleton
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Mockito and JUnit 5 – Using ExtendWith
Java Program to Print only Odd Numbered Levels of a Tree
Spring RestTemplate Error Handling
A Quick Guide to Using Keycloak with Spring Boot
How To Serialize and Deserialize Enums with Jackson
Lớp Collections trong Java (Collections Utility Class)