Skip to content

Commit b14db81

Browse files
Add shuffle array (#6026)
1 parent 857d921 commit b14db81

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.thealgorithms.misc;
2+
3+
import java.util.Random;
4+
5+
/**
6+
* The Fisher-Yates (Knuth) Shuffle algorithm randomly permutes an array's
7+
* elements, ensuring each permutation is equally likely.
8+
*
9+
* <p>
10+
* Worst-case performance O(n)
11+
* Best-case performance O(n)
12+
* Average performance O(n)
13+
* Worst-case space complexity O(1)
14+
*
15+
* This class provides a static method to shuffle an array in place.
16+
*
17+
* @author Rashi Dashore (https://github.com/rashi07dashore)
18+
*/
19+
public final class ShuffleArray {
20+
// Prevent instantiation
21+
private ShuffleArray() {
22+
}
23+
24+
/**
25+
* This method shuffles an array using the Fisher-Yates algorithm.
26+
*
27+
* @param arr is the input array to be shuffled
28+
*/
29+
public static void shuffle(int[] arr) {
30+
Random random = new Random();
31+
for (int i = arr.length - 1; i > 0; i--) {
32+
int j = random.nextInt(i + 1);
33+
int temp = arr[i];
34+
arr[i] = arr[j];
35+
arr[j] = temp;
36+
}
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.thealgorithms.misc;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
public class ShuffleArrayTest {
10+
11+
@Test
12+
void testShuffleBasic() {
13+
int[] arr = {1, 2, 3, 4, 5};
14+
int[] originalArr = arr.clone(); // Clone original array for comparison
15+
ShuffleArray.shuffle(arr);
16+
17+
// Check that the shuffled array is not the same as the original
18+
assertNotEquals(originalArr, arr);
19+
}
20+
21+
@Test
22+
void testShuffleSingleElement() {
23+
int[] arr = {1};
24+
int[] originalArr = arr.clone();
25+
ShuffleArray.shuffle(arr);
26+
27+
// Check that the shuffled array is the same as the original
28+
assertArrayEquals(originalArr, arr);
29+
}
30+
31+
@Test
32+
void testShuffleTwoElements() {
33+
int[] arr = {1, 2};
34+
int[] originalArr = arr.clone();
35+
ShuffleArray.shuffle(arr);
36+
37+
// Check that the shuffled array is not the same as the original
38+
assertNotEquals(originalArr, arr);
39+
// Check that the shuffled array still contains the same elements
40+
assertTrue(arr[0] == 1 || arr[0] == 2);
41+
assertTrue(arr[1] == 1 || arr[1] == 2);
42+
}
43+
44+
@Test
45+
void testShuffleEmptyArray() {
46+
int[] arr = {};
47+
int[] originalArr = arr.clone();
48+
ShuffleArray.shuffle(arr);
49+
50+
// Check that the shuffled array is the same as the original (still empty)
51+
assertArrayEquals(originalArr, arr);
52+
}
53+
54+
@Test
55+
void testShuffleLargeArray() {
56+
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
57+
int[] originalArr = arr.clone();
58+
ShuffleArray.shuffle(arr);
59+
60+
// Check that the shuffled array is not the same as the original
61+
assertNotEquals(originalArr, arr);
62+
}
63+
64+
@Test
65+
void testShuffleRetainsElements() {
66+
int[] arr = {1, 2, 3, 4, 5};
67+
ShuffleArray.shuffle(arr);
68+
69+
// Check that the shuffled array contains the same elements
70+
assertTrue(arr.length == 5);
71+
for (int i = 1; i <= 5; i++) {
72+
assertTrue(contains(arr, i));
73+
}
74+
}
75+
76+
private boolean contains(int[] arr, int value) {
77+
for (int num : arr) {
78+
if (num == value) {
79+
return true;
80+
}
81+
}
82+
return false;
83+
}
84+
}

0 commit comments

Comments
 (0)