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:
Spring Security – security none, filters none, access permitAll
Getting Started with Stream Processing with Spring Cloud Data Flow
Java Program to Implement Binomial Heap
Quick Guide to Spring Controllers
Java Copy Constructor
Cài đặt và sử dụng Swagger UI
Spring Boot: Customize the Jackson ObjectMapper
HttpClient 4 Cookbook
Get and Post Lists of Objects with RestTemplate
Summing Numbers with Java Streams
Finding the Differences Between Two Lists in Java
Date Time trong Java 8
DistinctBy in the Java Stream API
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
File Upload with Spring MVC
Filtering and Transforming Collections in Guava
Java Program to Implement Booth Algorithm
The Spring @Controller and @RestController Annotations
Query Entities by Dates and Times with Spring Data JPA
Java Program to Use Boruvka’s Algorithm to Find the Minimum Spanning Tree
Java 8 Predicate Chain
Case-Insensitive String Matching in Java
Java Program to Implement IdentityHashMap API
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Java Program to Implement TreeMap API
Spring Cloud – Adding Angular
Spring Security Registration – Resend Verification Email
Java Program to Implement Hash Tables with Quadratic Probing
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Java Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
Java Switch Statement
Java Program to Find Path Between Two Nodes in a Graph