Skip to content

Commit 0160f11

Browse files
done required changes
1 parent ead8be5 commit 0160f11

11 files changed

+650
-27
lines changed

src/main/java/com/thealgorithms/shufflealogrithm/ConstrainedShuffle.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ private ConstrainedShuffle() {
1010
}
1111

1212
/**
13-
* Shuffles elements in the array while ensuring that the first and last elements remain fixed.
13+
* Shuffles elements in the array while ensuring that the first and last
14+
* elements remain fixed.
1415
*
1516
* @param array the input array to shuffle
1617
*/

src/main/java/com/thealgorithms/shufflealogrithm/UnderstandingShuffleAlgo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ private UnderstandingShuffleAlgo() {
1010

1111
/**
1212
* Shuffles the elements in the array randomly.
13-
* Uses a method that gives each item an equal chance to appear in any position.
13+
* Uses a method that gives each item an equal chance to appear in any
14+
* position.
1415
*
1516
* @param array the array to be shuffled
1617
*/

src/main/java/com/thealgorithms/shufflealogrithm/UniquePairShuffle.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,21 @@ private UniquePairShuffle() {
1111
}
1212

1313
/**
14-
* Pairs each element in the array with another element randomly, ensuring no pair repeats.
15-
* If the array length is odd, pairing cannot be completed, so an empty list is returned.
14+
* Pairs each element in the array with another element randomly, ensuring no
15+
* pair repeats. If the array length is odd, pairing cannot be completed, so
16+
* an empty list is returned.
1617
*
1718
* @param array the input array to pair elements from
18-
* @return a list of unique pairs where each pair is represented as an integer array of length 2
19+
* @return a list of unique pairs where each pair is represented as an integer
20+
* array of length 2
1921
*/
2022
public static List<int[]> pairShuffle(int[] array) {
2123
List<int[]> pairs = new ArrayList<>();
2224

2325
// Handle edge case: If the array length is odd, pairing is not possible
24-
if (array.length % 2 != 0) return pairs;
26+
if (array.length % 2 != 0) {
27+
return pairs;
28+
}
2529

2630
List<Integer> shuffledList = new ArrayList<>();
2731
for (int num : array) {
@@ -33,7 +37,7 @@ public static List<int[]> pairShuffle(int[] array) {
3337

3438
// Form pairs from the shuffled elements
3539
for (int i = 0; i < shuffledList.size(); i += 2) {
36-
pairs.add(new int[]{shuffledList.get(i), shuffledList.get(i + 1)});
40+
pairs.add(new int[] {shuffledList.get(i), shuffledList.get(i + 1)});
3741
}
3842

3943
return pairs;

src/main/java/com/thealgorithms/shufflealogrithm/WeightedShuffle.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ private WeightedShuffle() {
1010
}
1111

1212
/**
13-
* Shuffles elements based on their weights. Higher weight elements are more likely to appear earlier.
13+
* Shuffles elements based on their weights. Higher weight elements are more
14+
* likely to appear earlier.
1415
*
1516
* @param array the input array to shuffle
1617
* @param weights the weights for each corresponding element in the array
@@ -26,7 +27,7 @@ public static void weightedShuffle(int[] array, int[] weights) {
2627
indices[i] = i;
2728
}
2829

29-
Arrays.sort(indices, Comparator.comparingInt(i -> -weights[i]));
30+
Arrays.sort(indices, Comparator.comparingInt(i -> - weights[i]));
3031

3132
int[] result = new int[array.length];
3233
for (int i = 0; i < array.length; i++) {
Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.thealgorithms.datastructures.trees;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
3+
import static org.junit.jupiter.api.Assertions.*;
44

55
import org.junit.jupiter.api.Test;
66

77
public class BinaryTreeTest {
88

9-
// checks that adding populating the tree and searching for data
10-
// retrieves the expected data
9+
// Test for adding elements and finding data within the tree
1110
@Test
1211
void test1() {
1312
BinaryTree t = new BinaryTree();
@@ -17,12 +16,14 @@ void test1() {
1716
t.put(9);
1817
t.put(12);
1918

20-
assertEquals(t.find(5).data, 5);
21-
assertEquals(t.find(7).data, 7);
19+
assertNotNull(t.find(5), "Node with value 5 should exist in the tree.");
20+
assertEquals(5, t.find(5).data);
21+
22+
assertNotNull(t.find(7), "Node with value 7 should exist in the tree.");
23+
assertEquals(7, t.find(7).data);
2224
}
2325

24-
// checks that removing data from the tree
25-
// properly removes and makes the new root the expected new root
26+
// Test for removing data and checking the new root
2627
@Test
2728
void test2() {
2829
BinaryTree t = new BinaryTree();
@@ -31,15 +32,17 @@ void test2() {
3132
t.put(7);
3233
t.put(9);
3334
t.put(12);
35+
36+
// Perform removals and check the new root
3437
t.remove(3);
3538
t.remove(5);
3639
t.remove(7);
3740

38-
assertEquals(t.getRoot().data, 9);
41+
assertNotNull(t.getRoot(), "Root should not be null after removals.");
42+
assertEquals(9, t.getRoot().data);
3943
}
4044

41-
// checks that removing an unexistend node returns false
42-
// as specified by the documentation of the function
45+
// Test for attempting to remove a nonexistent node
4346
@Test
4447
void test3() {
4548
BinaryTree t = new BinaryTree();
@@ -49,13 +52,12 @@ void test3() {
4952
t.put(9);
5053
t.put(12);
5154

52-
assertEquals(t.remove(9), true);
53-
assertEquals(t.remove(398745987), false);
55+
assertTrue(t.remove(9), "Node with value 9 should be removed.");
56+
assertFalse(t.remove(398745987),
57+
"Removing a nonexistent node should return false.");
5458
}
5559

56-
// check if the bfs, inOrder, preOrder and postOrder functions
57-
// worg as expected, also increases the coverage measures in
58-
// JaCoCo
60+
// Test traversal methods (bfs, inOrder, preOrder, postOrder)
5961
@Test
6062
void test4() {
6163
BinaryTree t = new BinaryTree();
@@ -65,12 +67,18 @@ void test4() {
6567
t.put(9);
6668
t.put(12);
6769

68-
t.bfs(t.find(12));
70+
// Ensure root is not null before traversal
71+
assertNotNull(t.getRoot(), "Root should not be null for traversal.");
72+
73+
// Invoke traversal methods to increase test coverage
74+
t.bfs(t.getRoot());
6975
t.inOrder(t.getRoot());
7076
t.preOrder(t.getRoot());
7177
t.postOrder(t.getRoot());
7278

73-
assertEquals(t.remove(9), true);
74-
assertEquals(t.remove(398745987), false);
79+
// Additional assertions
80+
assertTrue(t.remove(9), "Node with value 9 should be removed.");
81+
assertFalse(t.remove(398745987),
82+
"Removing a nonexistent node should return false.");
7583
}
7684
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.thealgorithms.shufflealgorithm;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
6+
import com.thealgorithms.shufflealogrithm.ConstrainedShuffle;
7+
import java.util.Arrays;
8+
import java.util.HashSet;
9+
import java.util.Set;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
13+
public class ConstrainedShuffleTest {
14+
15+
private int[] originalArray;
16+
17+
@BeforeEach
18+
void setUp() {
19+
originalArray = new int[] {1, 2, 3, 4, 5};
20+
}
21+
22+
// Test that shuffling preserves the length and original elements
23+
@Test
24+
void testPreserveArrayLengthAndElements() {
25+
int[] arrayCopy = Arrays.copyOf(originalArray, originalArray.length);
26+
ConstrainedShuffle.constrainedShuffle(arrayCopy);
27+
28+
assertEquals(originalArray.length, arrayCopy.length,
29+
"Array length should remain the same");
30+
31+
Set<Integer> originalElements = new HashSet<>();
32+
for (int num : originalArray) {
33+
originalElements.add(num);
34+
}
35+
36+
for (int num : arrayCopy) {
37+
assertEquals(true, originalElements.contains(num),
38+
"Array elements should be preserved");
39+
}
40+
}
41+
42+
// Test that the first and last elements remain unchanged after shuffle
43+
@Test
44+
void testFirstAndLastElementsFixed() {
45+
int[] arrayCopy = Arrays.copyOf(originalArray, originalArray.length);
46+
ConstrainedShuffle.constrainedShuffle(arrayCopy);
47+
48+
assertEquals(originalArray[0], arrayCopy[0],
49+
"First element should remain fixed");
50+
assertEquals(originalArray[originalArray.length - 1],
51+
arrayCopy[arrayCopy.length - 1],
52+
"Last element should remain fixed");
53+
}
54+
55+
// Test that the function handles arrays with fewer than 3 elements without
56+
// changes
57+
@Test
58+
void testArrayTooSmall() {
59+
int[] smallArray = {1, 2};
60+
int[] expectedArray = {1, 2};
61+
62+
ConstrainedShuffle.constrainedShuffle(smallArray);
63+
64+
assertArrayEquals(
65+
expectedArray, smallArray,
66+
"Array with fewer than 3 elements should remain unchanged");
67+
}
68+
69+
// Test with null input (should handle gracefully without error)
70+
@Test
71+
void testNullArray() {
72+
int[] nullArray = null;
73+
ConstrainedShuffle.constrainedShuffle(nullArray);
74+
75+
assertEquals(null, nullArray, "Null input should remain null");
76+
}
77+
78+
// Test that elements between the first and last positions change order in
79+
// larger arrays
80+
@Test
81+
void testInternalElementsShuffled() {
82+
int[] arrayCopy = Arrays.copyOf(originalArray, originalArray.length);
83+
boolean hasShuffled = false;
84+
85+
for (int i = 0; i < 10; i++) { // Repeat shuffle to ensure randomness
86+
ConstrainedShuffle.constrainedShuffle(arrayCopy);
87+
88+
if (!Arrays.equals(arrayCopy, originalArray)) {
89+
hasShuffled = true;
90+
break;
91+
}
92+
}
93+
94+
assertEquals(
95+
true, hasShuffled,
96+
"Internal elements should shuffle between first and last positions");
97+
}
98+
99+
// Test with an array that has all identical elements
100+
@Test
101+
void testArrayWithIdenticalElements() {
102+
int[] identicalArray = {1, 1, 1, 1, 1};
103+
int[] expectedArray = {1, 1, 1, 1, 1};
104+
105+
ConstrainedShuffle.constrainedShuffle(identicalArray);
106+
107+
assertArrayEquals(
108+
expectedArray, identicalArray,
109+
"Array with all identical elements should remain unchanged");
110+
}
111+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.thealgorithms.shufflealgorithm;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import com.thealgorithms.shufflealogrithm.GroupShuffle;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class GroupShuffleTest {
12+
13+
// Test case to check basic functionality
14+
@Test
15+
void testGroupShuffleBasic() {
16+
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
17+
List<List<Integer>> shuffledGroups = GroupShuffle.groupShuffle(array, 3);
18+
19+
assertEquals(3, shuffledGroups.size()); // Expect 3 groups
20+
assertTrue(shuffledGroups.stream().allMatch(
21+
group -> group.size() <= 3)); // All groups should have size <= 3
22+
System.out.println("Shuffled Groups (Basic Test): " + shuffledGroups);
23+
}
24+
25+
// Test case to check when group size is larger than array length
26+
@Test
27+
void testGroupShuffleLargeGroupSize() {
28+
int[] array = {1, 2, 3};
29+
List<List<Integer>> shuffledGroups = GroupShuffle.groupShuffle(array, 5);
30+
31+
assertEquals(1, shuffledGroups.size()); // Expect 1 group with all elements
32+
assertEquals(
33+
Arrays.asList(1, 2, 3),
34+
shuffledGroups.get(0)); // The group should contain all elements
35+
System.out.println("Shuffled Groups (Large Group Size Test): " +
36+
shuffledGroups);
37+
}
38+
39+
// Test case to check when the array is null
40+
@Test
41+
void testGroupShuffleNullArray() {
42+
List<List<Integer>> shuffledGroups = GroupShuffle.groupShuffle(null, 3);
43+
44+
assertTrue(shuffledGroups.isEmpty()); // Expect empty list
45+
System.out.println("Shuffled Groups (Null Array Test): " + shuffledGroups);
46+
}
47+
48+
// Test case to check when group size is less than or equal to zero
49+
@Test
50+
void testGroupShuffleZeroOrNegativeGroupSize() {
51+
int[] array = {1, 2, 3};
52+
List<List<Integer>> shuffledGroups = GroupShuffle.groupShuffle(array, 0);
53+
54+
assertTrue(
55+
shuffledGroups.isEmpty()); // Expect empty list for group size zero
56+
shuffledGroups = GroupShuffle.groupShuffle(array, -1);
57+
assertTrue(
58+
shuffledGroups.isEmpty()); // Expect empty list for negative group size
59+
System.out.println("Shuffled Groups (Zero or Negative Group Size Test): " +
60+
shuffledGroups);
61+
}
62+
63+
// Test case to check when the array has fewer than 3 elements
64+
@Test
65+
void testGroupShuffleSmallArray() {
66+
int[] array = {1, 2};
67+
List<List<Integer>> shuffledGroups = GroupShuffle.groupShuffle(array, 2);
68+
69+
assertEquals(1, shuffledGroups.size()); // Expect 1 group with all elements
70+
assertEquals(
71+
Arrays.asList(1, 2),
72+
shuffledGroups.get(0)); // The group should contain all elements
73+
System.out.println("Shuffled Groups (Small Array Test): " + shuffledGroups);
74+
}
75+
76+
// Test case to check the behavior when the group size is 1
77+
@Test
78+
void testGroupShuffleGroupSizeOne() {
79+
int[] array = {1, 2, 3, 4, 5};
80+
List<List<Integer>> shuffledGroups = GroupShuffle.groupShuffle(array, 1);
81+
82+
assertEquals(5, shuffledGroups.size()); // Expect 5 groups
83+
for (int i = 0; i < 5; i++) {
84+
assertEquals(
85+
Arrays.asList(i + 1),
86+
shuffledGroups.get(i)); // Each group should contain one element
87+
}
88+
System.out.println("Shuffled Groups (Group Size One Test): " +
89+
shuffledGroups);
90+
}
91+
}

0 commit comments

Comments
 (0)