Java Program To Jumble an Array Last Updated : 24 Nov, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an array of size N, and the task is to shuffle the elements of the array or any other permutation. Using Java think of stuffing the array by generating random sequences of the elements present in the array is possible. This Algorithm is called Fisher-Yates Shuffle Algorithm. Fisher–Yates shuffle Algorithm Works in O(n) time complexity. The assumption is that a give a function rand() generates a random number in O(1) time. Start from the last element, swap it with a randomly selected element from the whole array. Now consider the array from 0 to n-2 (size reduced by 1), and repeat till we hit the first element. Example: Input : arr[] = {1, 2, 3, 4} Output: arr[] = {3, 2, 4, 1} Input : arr[] = {5, 2, 3, 4} Output: arr[] = {2, 4, 3, 5} Algorithm: for i from n - 1 downto 1 do j = random integer with 0 <= j <= i exchange a[j] and a[i] Below is the implementation of the above approach: Java // Program to jumble an array using Java import java.util.Random; import java.io.*; public class GFG { public static void shuffleanarray(int[] a) { int n = a.length; Random random = new Random(); // generating random number from list random.nextInt(); for (int i = 0; i < n; i++) { // using random generated number int change = i + random.nextInt(n - i); // swapping elements to shuffle int holder = a[i]; a[i] = a[change]; a[change] = holder; } } public static void main(String[] args) { int[] a = new int[] { 0, 1, 2, 3, 4, 5, 6 }; shuffleanarray(a); System.out.print("arr[] = {"); for (int i : a) { System.out.print(i + " "); } System.out.print("}"); } } Outputarr[] = {4 0 6 1 5 3 2 } Time Complexity: O(N), where N is the size of an array. Comment More infoAdvertise with us Next Article Java Program To Jumble an Array A apurva27299 Follow Improve Article Tags : Misc Java Java Programs Java-Array-Programs Practice Tags : JavaMisc Similar Reads Java Program to Merge Two Arrays In Java, merging two arrays is a good programming question. We have given two arrays, and our task is to merge them, and after merging, we need to put the result back into another array. In this article, we are going to discuss different ways we can use to merge two arrays in Java.Let's now see the 5 min read Java Array Programs An array is a data structure consisting of a collection of elements (values or variables), of the same memory size, each identified by at least one array index or key. An array is a linear data structure that stores similar elements (i.e. elements of similar data type) that are stored in contiguous 4 min read Java Program to Convert an Array into a List In Java, arrays and lists are two commonly used data structures. While arrays have a fixed size and are simple to use, lists are dynamic and provide more flexibility. There are times when you may need to convert an array into a list, for instance, when you want to perform operations like adding or r 4 min read Java Program to Print the Elements of an Array An array is a data structure that stores a collection of like-typed variables in contiguous memory allocation. Once created, the size of an array in Java cannot be changed. It's important to note that arrays in Java function differently than they do in C/C++As you see, the array of size 9 holds elem 6 min read Java Program to Left Rotate the Elements of an Array In Java, left rotation of an array involves shifting its elements to the left by a given number of positions, with the first elements moving around to the end. There are different ways to left rotate the elements of an array in Java.Example: We can use a temporary array to rotate the array left by " 5 min read Like