Javatpoint Logo
Javatpoint Logo

Shuffle an Array in Java

In the world of programming, manipulating arrays is a fundamental skill. An array can be shuffled, which includes randomly rearranging its elements, as one common process. This procedure is essential for things like building randomized gaming decks, running statistical simulations, or just displaying data more randomly. Initially, there is much logic we can apply to shuffle an array; we can use different types of collection frameworks such as ArrayList, hash sets, linked lists, etc. shuffling of an array can be done differently and

Algorithm for Shuffling an Array:

The following is the algorithm for the shuffle of an array,

STEP 1: START

STEP 2: Start from the last element of the array and go backward to the first element.

STEP 3: For each element at index i, generate a random index j such that j is in the range [0, i].

STEP 4: Swap the elements at indices i and j.

STEP 5: Repeat steps 2 and 3 for all elements in the array, moving backward from the last element to the first.

STEP 6: END

We can shuffle an array containing different types of elements like integers, characters, etc.

Fisher-yates Shuffle Algorithm:

The following Java program is used to shuffle an array consists integers.

ArrayShuffle.java

Output:

1 3 2 4 5

The output may differ if you execute it in your system because it randomly arranges the elements and outputs the shuffled array.

Complexities:

The space complexity of the shuffle algorithm is O(1) because it does not use any extra data structures that depend on the size of the array. The time complexity of the Fisher-Yates shuffle algorithm used in the shuffleArray() method is O(n), where n is the number of elements in the array.

Shuffling Array Using Lists in Java:

ShuffleArray.java

Output:

[4, 1, 7, 3, 6, 5, 2]

The output may differ if you execute it in your system because it randomly arranges the elements and outputs the shuffled array.

Complexities:

The space complexity is O(n) as well. This is because the Collections.shuffle() method modifies the original list in place and does not use any additional data structures. The time complexity of this code is O(n), where n is the number of elements in the array.

Shuffle Array Containing Characters:

ShuffleCharacters.java

Output:

Shuffled Characters: [e, f, g, d, a, c, b]

The output may differ if you execute it in your system because it randomly arranges the elements and outputs the shuffled array.

Complexities:

The space complexity of the shuffle algorithm is O(1) because it does not use any extra data structures that depend on the size of the array. The time complexity of the program used in the shuffleArray() method is O(n), where n is the number of characters in the array.

Conclusion:

Shuffling an array in Java is a crucial skill that empowers developers to create randomized and unbiased arrangements of data. Throughout this exploration, we've covered two effective approaches: using the Collections.shuffle() method for non-primitive arrays and implementing the Fisher-Yates shuffle algorithm for primitive arrays. The Collections.shuffle() method simplifies the shuffling process for objects or non-primitive arrays by leveraging built-in functionalities. On the other hand, the Fisher-Yates algorithm provides an efficient and unbiased way to shuffle primitive arrays, ensuring uniformity in permutations.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA