diff --git a/src/main/java/com/thealgorithms/shufflealgo/ConstrainedShuffle.java b/src/main/java/com/thealgorithms/shufflealgo/ConstrainedShuffle.java new file mode 100644 index 000000000000..ee391067d5e0 --- /dev/null +++ b/src/main/java/com/thealgorithms/shufflealgo/ConstrainedShuffle.java @@ -0,0 +1,48 @@ +package com.thealgorithms.shufflealgo; + +import java.util.Arrays; +import java.util.Random; + +public +final class ConstrainedShuffle { + + private + ConstrainedShuffle() { + // Prevent instantiation + } + + /** + * Shuffles elements in the array while ensuring that the first and last + * elements remain fixed. + * + * @param array the input array to shuffle + */ + public + static void constrainedShuffle(int[] array) { + // Edge case: If the array has less than 3 elements, no shuffling can occur + if (array == null || array.length < 3) { + return; + } + + Random random = new Random(); + // Start loop from the second last element and exclude the first and last + // elements + for (int i = array.length - 2; i > 1; i--) { + // Generate a random index between 1 and i (inclusive) + int j = random.nextInt(i - 1) + 1; + + // Swap the elements at positions i and j + int temp = array[i]; + array[i] = array[j]; + array[j] = temp; + } + } + + public + static void main(String[] args) { + int[] array = {1, 2, 3, 4, 5}; + System.out.println("Original Array: " + Arrays.toString(array)); + constrainedShuffle(array); + System.out.println("Constrained Shuffled Array: " + Arrays.toString(array)); + } +} diff --git a/src/main/java/com/thealgorithms/shufflealgo/GroupShuffle.java b/src/main/java/com/thealgorithms/shufflealgo/GroupShuffle.java new file mode 100644 index 000000000000..f7b109947f1b --- /dev/null +++ b/src/main/java/com/thealgorithms/shufflealgo/GroupShuffle.java @@ -0,0 +1,57 @@ +package com.thealgorithms.shufflealgo; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public +final class GroupShuffle { + + private + GroupShuffle() { + // Prevent instantiation + } + + /** + * Groups and shuffles elements in the array. + * + * @param array the input array to shuffle + * @param groupSize the size of each group + * @return a list of shuffled groups + */ + public + static List> groupShuffle(int[] array, int groupSize) { + List> groups = new ArrayList<>(); + + // Edge case: Check if the group size is valid + if (array == null || groupSize <= 0) { + return groups; + } + + for (int i = 0; i < array.length; i += groupSize) { + List group = new ArrayList<>(); + for (int j = i; j < Math.min(i + groupSize, array.length); j++) { + group.add(array[j]); + } + groups.add(group); + } + + // Shuffle only if the group size is greater than 1 + if (groupSize > 1) { + Collections.shuffle(groups); + } + + return groups; + } + + public + static void main(String[] args) { + int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + List> shuffledGroups = groupShuffle(array, 3); + + System.out.println("Shuffled Groups:"); + for (List group : shuffledGroups) { + System.out.println(group); + } + } +} diff --git a/src/main/java/com/thealgorithms/shufflealgo/ShuffleByRange.java b/src/main/java/com/thealgorithms/shufflealgo/ShuffleByRange.java new file mode 100644 index 000000000000..e1bced3723b0 --- /dev/null +++ b/src/main/java/com/thealgorithms/shufflealgo/ShuffleByRange.java @@ -0,0 +1,52 @@ +package com.thealgorithms.shufflealgo; + +import java.util.Random; + +public +final class ShuffleByRange { + + private + ShuffleByRange() { + // Prevent instantiation + } + + /** + * Shuffles elements in the specified range of the array. + * + * @param array the input array to shuffle + * @param start the starting index of the range (inclusive) + * @param end the ending index of the range (exclusive) + */ + public + static void shuffleByRange(int[] array, int start, int end) { + // Edge case: Check if the range is valid + if (array == null || start < 0 || end > array.length || start >= end) { + return; + } + + Random random = new Random(); + for (int i = end - 1; i > start; i--) { + int j = random.nextInt(i - start + 1) + start; + + // Swap the elements at positions i and j + int temp = array[i]; // Temporarily store the element at i + array[i] = array[j]; // Move element from j to i + array[j] = temp; // Place the stored element in position j + } + } + + public + static void main(String[] args) { + int[] array = {1, 2, 3, 4, 5, 6}; + System.out.println("Original Array: "); + for (int num : array) { + System.out.print(num + " "); + } + + shuffleByRange(array, 1, 5); + System.out.println("\nShuffled Array (Range 1 to 5): "); + for (int num : array) { + System.out.print(num + " "); + } + } +} diff --git a/src/main/java/com/thealgorithms/shufflealgo/UnderstandingShuffleAlgo.java b/src/main/java/com/thealgorithms/shufflealgo/UnderstandingShuffleAlgo.java new file mode 100644 index 000000000000..52071f6b5bcd --- /dev/null +++ b/src/main/java/com/thealgorithms/shufflealgo/UnderstandingShuffleAlgo.java @@ -0,0 +1,63 @@ +package com.thealgorithms.shufflealgo; + +import java.util.Random; + +public +final class UnderstandingShuffleAlgo { + + private + UnderstandingShuffleAlgo() { + // Prevent instantiation + } + + /** + * Shuffles the elements in the array randomly. + * Uses a method that gives each item an equal chance to appear in any + * position. + * + * @param array the array to be shuffled + */ + public + static void shuffle(int[] array) { + // Create a Random object to generate random numbers + Random random = new Random(); + + // Loop from the last element to the second element + for (int i = array.length - 1; i > 0; i--) { + // Generate a random index from 0 to i (inclusive) + int j = random.nextInt(i + 1); + + // Swap the elements at positions i and j + int temp = array[i]; // Temporarily store the element at i + array[i] = array[j]; // Move element from j to i + array[j] = temp; // Place the stored element in position j + } + } + + /** + * Main method to demonstrate the shuffle function. + * Shows the array before and after shuffling. + * + * @param args command-line arguments (not used here) + */ + public + static void main(String[] args) { + // Create an example array of numbers from 1 to 9 + int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + + // Display the original array + System.out.println("Original Array:"); + for (int num : array) { + System.out.print(num + " "); + } + + // Call the shuffle method to randomize the array + shuffle(array); + + // Display the shuffled array + System.out.println("\nShuffled Array:"); + for (int num : array) { + System.out.print(num + " "); + } + } +} diff --git a/src/main/java/com/thealgorithms/shufflealgo/UniquePairShuffle.java b/src/main/java/com/thealgorithms/shufflealgo/UniquePairShuffle.java new file mode 100644 index 000000000000..47c148b466f0 --- /dev/null +++ b/src/main/java/com/thealgorithms/shufflealgo/UniquePairShuffle.java @@ -0,0 +1,59 @@ +package com.thealgorithms.shufflealgo; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public +final class UniquePairShuffle { + + private + UniquePairShuffle() { + // Prevent instantiation + } + + /** + * Pairs each element in the array with another element randomly, ensuring no + * pair repeats. If the array length is odd, pairing cannot be completed, so + * an empty list is returned. + * + * @param array the input array to pair elements from + * @return a list of unique pairs where each pair is represented as an integer + * array of length 2 + */ + public + static List pairShuffle(int[] array) { + List pairs = new ArrayList<>(); + + // Handle edge case: If the array length is odd, pairing is not possible + if (array.length < 2 || array.length % 2 != 0) { + return pairs; + } + + List shuffledList = new ArrayList<>(); + for (int num : array) { + shuffledList.add(num); + } + + // Shuffle elements to create random pairs + Collections.shuffle(shuffledList); + + // Form pairs from the shuffled elements + for (int i = 0; i < shuffledList.size(); i += 2) { + pairs.add(new int[]{shuffledList.get(i), shuffledList.get(i + 1)}); + } + + return pairs; + } + + public + static void main(String[] args) { + int[] array = {1, 2, 3, 4}; + List pairs = pairShuffle(array); + + System.out.println("Generated Unique Pairs:"); + for (int[] pair : pairs) { + System.out.println(pair[0] + " - " + pair[1]); + } + } +} diff --git a/src/main/java/com/thealgorithms/shufflealgo/WeightedShuffle.java b/src/main/java/com/thealgorithms/shufflealgo/WeightedShuffle.java new file mode 100644 index 000000000000..17b23d526b25 --- /dev/null +++ b/src/main/java/com/thealgorithms/shufflealgo/WeightedShuffle.java @@ -0,0 +1,60 @@ +package com.thealgorithms.shufflealgo; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.Random; + +public +final class WeightedShuffle { + + private + WeightedShuffle() { + // Prevent instantiation + } + + /** + * Shuffles elements based on their weights. Higher weight elements are more + * likely to appear earlier. + * + * @param array the input array to shuffle + * @param weights the weights for each corresponding element in the array + */ + public + static void weightedShuffle(int[] array, int[] weights) { + // Edge case: Check if weights match the array size + if (array == null || weights == null || array.length != weights.length) { + return; + } + + Integer[] indices = new Integer[array.length]; + for (int i = 0; i < array.length; i++) { + indices[i] = i; + } + + Random random = new Random(); + + // Sort indices by weights in descending order, prioritizing higher weights + Arrays.sort(indices, Comparator.comparingInt((Integer i)->- weights[i]) + .thenComparingInt(i->random.nextInt())); + + int[] result = new int[array.length]; + for (int i = 0; i < array.length; i++) { + result[i] = array[indices[i]]; + } + + System.arraycopy(result, 0, array, 0, array.length); + } + + public + static void main(String[] args) { + int[] array = {10, 20, 30}; + int[] weights = {1, 3, 2}; + weightedShuffle(array, weights); + + System.out.println("Weighted Shuffled Array:"); + for (int num : array) { + System.out.print(num + " "); + } + } +} + diff --git a/src/test/java/com/thealgorithms/shufflealgo/ConstrainedShuffleTest.java b/src/test/java/com/thealgorithms/shufflealgo/ConstrainedShuffleTest.java new file mode 100644 index 000000000000..c5d2c078412d --- /dev/null +++ b/src/test/java/com/thealgorithms/shufflealgo/ConstrainedShuffleTest.java @@ -0,0 +1,107 @@ +package com.thealgorithms.shufflealgo; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public +class ConstrainedShuffleTest { + + private + int[] originalArray; + + @BeforeEach void setUp() { + originalArray = new int[]{1, 2, 3, 4, 5}; + } + + // Test that shuffling preserves the length and original elements + @Test void testPreserveArrayLengthAndElements() { + int[] arrayCopy = Arrays.copyOf(originalArray, originalArray.length); + ConstrainedShuffle.constrainedShuffle(arrayCopy); + + assertEquals(originalArray.length, arrayCopy.length, + "Array length should remain the same"); + + Set originalElements = new HashSet<>(); + for (int num : originalArray) { + originalElements.add(num); + } + + for (int num : arrayCopy) { + assertTrue(originalElements.contains(num), + "Array elements should be preserved"); + } + } + + // Test that the first and last elements remain unchanged after shuffle + @Test void testFirstAndLastElementsFixed() { + int[] arrayCopy = Arrays.copyOf(originalArray, originalArray.length); + ConstrainedShuffle.constrainedShuffle(arrayCopy); + + assertEquals(originalArray[0], arrayCopy[0], + "First element should remain fixed"); + assertEquals(originalArray[originalArray.length - 1], + arrayCopy[arrayCopy.length - 1], + "Last element should remain fixed"); + } + + // Test that the function handles arrays with fewer than 3 elements without + // changes + @Test void testArrayTooSmall() { + int[] smallArray = {1, 2}; + int[] expectedArray = {1, 2}; + + ConstrainedShuffle.constrainedShuffle(smallArray); + + assertArrayEquals( + expectedArray, smallArray, + "Array with fewer than 3 elements should remain unchanged"); + } + + // Test with null input (should handle gracefully without error) + @Test void testNullArray() { + int[] nullArray = null; + ConstrainedShuffle.constrainedShuffle(nullArray); + + assertEquals(null, nullArray, "Null input should remain null"); + } + + // Test that elements between the first and last positions change order in + // larger arrays + @Test void testInternalElementsShuffled() { + int[] arrayCopy = Arrays.copyOf(originalArray, originalArray.length); + boolean hasShuffled = false; + + for (int i = 0; i < 10; i++) { // Repeat shuffle to ensure randomness + ConstrainedShuffle.constrainedShuffle(arrayCopy); + + if (!Arrays.equals(arrayCopy, originalArray)) { + hasShuffled = true; + break; + } + } + + assertTrue( + hasShuffled, + "Internal elements should shuffle between first and last positions"); + } + + // Test with an array that has all identical elements + @Test void testArrayWithIdenticalElements() { + int[] identicalArray = {1, 1, 1, 1, 1}; + int[] expectedArray = {1, 1, 1, 1, 1}; + + ConstrainedShuffle.constrainedShuffle(identicalArray); + + assertArrayEquals( + expectedArray, identicalArray, + "Array with all identical elements should remain unchanged"); + } +} diff --git a/src/test/java/com/thealgorithms/shufflealgo/GroupShuffleTest.java b/src/test/java/com/thealgorithms/shufflealgo/GroupShuffleTest.java new file mode 100644 index 000000000000..21ff64cce541 --- /dev/null +++ b/src/test/java/com/thealgorithms/shufflealgo/GroupShuffleTest.java @@ -0,0 +1,86 @@ +package com.thealgorithms.shufflealgo; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Arrays; +import java.util.List; + +import org.junit.jupiter.api.Test; + +public +class GroupShuffleTest { + + // Test case to check basic functionality + @Test void testGroupShuffleBasic() { + int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + List> shuffledGroups = GroupShuffle.groupShuffle(array, 3); + + assertEquals(3, shuffledGroups.size()); // Expect 3 groups + assertTrue(shuffledGroups.stream().allMatch( + group->group.size() <= 3)); // All groups should have size <= 3 + System.out.println("Shuffled Groups (Basic Test): " + shuffledGroups); + } + + // Test case to check when group size is larger than array length + @Test void testGroupShuffleLargeGroupSize() { + int[] array = {1, 2, 3}; + List> shuffledGroups = GroupShuffle.groupShuffle(array, 5); + + assertEquals(1, shuffledGroups.size()); // Expect 1 group with all elements + assertEquals( + Arrays.asList(1, 2, 3), + shuffledGroups.get(0)); // The group should contain all elements + System.out.println("Shuffled Groups (Large Group Size Test): " + + shuffledGroups); + } + + // Test case to check when the array is null + @Test void testGroupShuffleNullArray() { + List> shuffledGroups = GroupShuffle.groupShuffle(null, 3); + + assertTrue(shuffledGroups.isEmpty()); // Expect empty list + System.out.println("Shuffled Groups (Null Array Test): " + shuffledGroups); + } + + // Test case to check when group size is less than or equal to zero + @Test void testGroupShuffleZeroOrNegativeGroupSize() { + int[] array = {1, 2, 3}; + List> shuffledGroups = GroupShuffle.groupShuffle(array, 0); + + assertTrue( + shuffledGroups.isEmpty()); // Expect empty list for group size zero + shuffledGroups = GroupShuffle.groupShuffle(array, -1); + assertTrue( + shuffledGroups.isEmpty()); // Expect empty list for negative group size + System.out.println("Shuffled Groups (Zero or Negative Group Size Test): " + + shuffledGroups); + } + + // Test case to check when the array has fewer than 3 elements + @Test void testGroupShuffleSmallArray() { + int[] array = {1, 2}; + List> shuffledGroups = GroupShuffle.groupShuffle(array, 2); + + assertEquals(1, shuffledGroups.size()); // Expect 1 group with all elements + assertEquals( + Arrays.asList(1, 2), + shuffledGroups.get(0)); // The group should contain all elements + System.out.println("Shuffled Groups (Small Array Test): " + shuffledGroups); + } + + // Test case to check the behavior when the group size is 1 + @Test void testGroupShuffleGroupSizeOne() { + int[] array = {1, 2, 3, 4, 5}; + List> shuffledGroups = GroupShuffle.groupShuffle(array, 1); + + assertEquals(5, shuffledGroups.size()); // Expect 5 groups + for (int i = 0; i < 5; i++) { + assertEquals( + Arrays.asList(i + 1), + shuffledGroups.get(i)); // Each group should contain one element + } + System.out.println("Shuffled Groups (Group Size One Test): " + + shuffledGroups); + } +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/shufflealgo/ShuffleByRangeTest.java b/src/test/java/com/thealgorithms/shufflealgo/ShuffleByRangeTest.java new file mode 100644 index 000000000000..9cdc4b7c4661 --- /dev/null +++ b/src/test/java/com/thealgorithms/shufflealgo/ShuffleByRangeTest.java @@ -0,0 +1,79 @@ +package com.thealgorithms.shufflealgo; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Assertions; + +public +class ShuffleByRangeTest { + + // Test case for a basic shuffle within a valid range + @Test void testShuffleByRangeBasic() { + int[] array = {1, 2, 3, 4, 5, 6}; + ShuffleByRange.shuffleByRange(array, 1, 5); + + // Verify that elements outside the specified range remain unchanged + Assertions.assertEquals(1, array[0], "First element should be unchanged"); + Assertions.assertEquals(6, array[5], "Last element should be unchanged"); + } + + // Test case for an empty array + @Test void testShuffleByRangeEmptyArray() { + int[] array = {}; + ShuffleByRange.shuffleByRange(array, 0, 1); + Assertions.assertArrayEquals(new int[]{}, array); + } + + // Test case for a single element array + @Test void testShuffleByRangeSingleElementArray() { + int[] array = {1}; + ShuffleByRange.shuffleByRange(array, 0, 1); + Assertions.assertArrayEquals(new int[]{1}, array); + } + + // Test case for invalid range: start index equal to end index + @Test void testShuffleByRangeStartEqualsEnd() { + int[] array = {1, 2, 3, 4, 5}; + ShuffleByRange.shuffleByRange(array, 2, 2); + Assertions.assertArrayEquals(new int[]{1, 2, 3, 4, 5}, array); + } + + // Test case for invalid range: start index out of bounds + @Test void testShuffleByRangeStartOutOfBounds() { + int[] array = {1, 2, 3, 4, 5}; + ShuffleByRange.shuffleByRange(array, -1, 5); + Assertions.assertArrayEquals(new int[]{1, 2, 3, 4, 5}, array); + } + + // Test case for invalid range: end index out of bounds + @Test void testShuffleByRangeEndOutOfBounds() { + int[] array = {1, 2, 3, 4, 5}; + ShuffleByRange.shuffleByRange(array, 1, 6); + Assertions.assertArrayEquals(new int[]{1, 2, 3, 4, 5}, array); + } + + // Test case for invalid range: start index greater than end index + @Test void testShuffleByRangeStartGreaterThanEnd() { + int[] array = {1, 2, 3, 4, 5}; + ShuffleByRange.shuffleByRange(array, 3, 2); + Assertions.assertArrayEquals(new int[]{1, 2, 3, 4, 5}, array); + } + + // Test case for shuffling a large array + @Test void testShuffleByRangeLargeArray() { + int[] array = new int[1000]; + for (int i = 0; i < array.length; i++) { + array[i] = i + 1; // Filling array with values 1 to 1000 + } + ShuffleByRange.shuffleByRange(array, 250, 750); + + // Verify that the first 250 and last 250 elements remain unchanged + for (int i = 0; i < 250; i++) { + Assertions.assertEquals( + i + 1, array[i], "Elements at index " + i + " should be unchanged"); + } + for (int i = 750; i < 1000; i++) { + Assertions.assertEquals( + i + 1, array[i], "Elements at index " + i + " should be unchanged"); + } + } +} diff --git a/src/test/java/com/thealgorithms/shufflealgo/UnderstandingShuffleAlgoTest.java b/src/test/java/com/thealgorithms/shufflealgo/UnderstandingShuffleAlgoTest.java new file mode 100644 index 000000000000..ea8946b9ea16 --- /dev/null +++ b/src/test/java/com/thealgorithms/shufflealgo/UnderstandingShuffleAlgoTest.java @@ -0,0 +1,91 @@ +package com.thealgorithms.shufflealgo; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.Arrays; + +import org.junit.jupiter.api.Test; + +public +class UnderstandingShuffleAlgoTest { + + // Test case for basic functionality with a standard array + @Test void testShuffleStandardArray() { + int[] array = {1, 2, 3, 4, 5}; + int[] originalArray = Arrays.copyOf(array, array.length); + + UnderstandingShuffleAlgo.shuffle(array); + + // Check that the shuffled array contains the same elements + Arrays.sort(originalArray); + Arrays.sort(array); + assertArrayEquals(originalArray, array); + } + + // Test case for an empty array + @Test void testShuffleEmptyArray() { + int[] array = {}; + UnderstandingShuffleAlgo.shuffle(array); + assertArrayEquals(new int[]{}, array); // Should remain empty + } + + // Test case for a single element array + @Test void testShuffleSingleElementArray() { + int[] array = {1}; + UnderstandingShuffleAlgo.shuffle(array); + assertArrayEquals(new int[]{1}, array); // Should remain unchanged + } + + // Test case for a null array + @Test void testShuffleNullArray() { + int[] array = null; + assertThrows( + NullPointerException.class, ()->{ + UnderstandingShuffleAlgo.shuffle( + array); // Expect a NullPointerException + }); + } + + // Test case for an array with duplicate elements + @Test void testShuffleArrayWithDuplicates() { + int[] array = {1, 1, 2, 2, 3, 3}; + int[] originalArray = Arrays.copyOf(array, array.length); + + UnderstandingShuffleAlgo.shuffle(array); + + // Check that the shuffled array contains the same elements + Arrays.sort(originalArray); + Arrays.sort(array); + assertArrayEquals(originalArray, array); + } + + // Test case for large array + @Test void testShuffleLargeArray() { + int[] array = new int[1000]; + for (int i = 0; i < array.length; i++) { + array[i] = i + 1; // Fill with numbers 1 to 1000 + } + + int[] originalArray = Arrays.copyOf(array, array.length); + UnderstandingShuffleAlgo.shuffle(array); + + // Check that the shuffled array contains the same elements + Arrays.sort(originalArray); + Arrays.sort(array); + assertArrayEquals(originalArray, array); + } + + // Test case for an already sorted array + @Test void testShuffleSortedArray() { + int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + int[] originalArray = Arrays.copyOf(array, array.length); + + UnderstandingShuffleAlgo.shuffle(array); + + // Check that the shuffled array contains the same elements + Arrays.sort(originalArray); + Arrays.sort(array); + assertArrayEquals(originalArray, array); + } +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/shufflealgo/UniquePairShuffleTest.java b/src/test/java/com/thealgorithms/shufflealgo/UniquePairShuffleTest.java new file mode 100644 index 000000000000..78f0b4d5356b --- /dev/null +++ b/src/test/java/com/thealgorithms/shufflealgo/UniquePairShuffleTest.java @@ -0,0 +1,128 @@ +package com.thealgorithms.shufflealgo; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.junit.jupiter.api.Test; + +public +class UniquePairShuffleTest { + + // Test case for standard even-length array + @Test void testPairShuffleEvenArray() { + int[] array = {1, 2, 3, 4}; + List pairs = UniquePairShuffle.pairShuffle(array); + + // There should be 2 pairs in the result + assertEquals(2, pairs.size()); + + // Check that all pairs are unique + Set uniquePairs = new HashSet<>(); + for (int[] pair : pairs) { + String pairString = + Math.min(pair[0], pair[1]) + "-" + Math.max(pair[0], pair[1]); + uniquePairs.add(pairString); + } + + assertEquals(2, uniquePairs.size()); // Should have 2 unique pairs + } + + // Test case for standard odd-length array + @Test void testPairShuffleOddArray() { + int[] array = {1, 2, 3}; + List pairs = UniquePairShuffle.pairShuffle(array); + + // Result should be empty for odd-length array + assertTrue(pairs.isEmpty()); + } + + // Test case for empty array + @Test void testPairShuffleEmptyArray() { + int[] array = {}; + List pairs = UniquePairShuffle.pairShuffle(array); + + // Result should be empty + assertTrue(pairs.isEmpty()); + } + + // Test case for single element array + @Test void testPairShuffleSingleElementArray() { + int[] array = {1}; + List pairs = UniquePairShuffle.pairShuffle(array); + + // Result should be empty for single-element array + assertTrue(pairs.isEmpty()); + } + + // Test case for two elements, adjusted to not expect a specific order + @Test void testPairShuffleTwoElements() { + int[] array = {1, 2}; + List pairs = UniquePairShuffle.pairShuffle(array); + + // There should be 1 pair in the result + assertEquals(1, pairs.size()); + int[] pair = pairs.get(0); + + // Check that the pair contains both elements, without specifying order + assertTrue((pair[0] == 1 && pair[1] == 2) || + (pair[0] == 2 && pair[1] == 1)); + + // Alternatively, for clarity and to directly address the error, + // you could assert the pair's elements without considering order: + int min = Math.min(pair[0], pair[1]); + int max = Math.max(pair[0], pair[1]); + assertEquals(1, min, "First element of the pair should be 1 when ordered"); + assertEquals(2, max, "Second element of the pair should be 2 when ordered"); + } + + // Test case for larger even-length array + @Test void testPairShuffleLargeEvenArray() { + int[] array = {1, 2, 3, 4, 5, 6, 7, 8}; + List pairs = UniquePairShuffle.pairShuffle(array); + + // There should be 4 pairs in the result + assertEquals(4, pairs.size()); + + // Check that all pairs are unique + Set uniquePairs = new HashSet<>(); + for (int[] pair : pairs) { + String pairString = + Math.min(pair[0], pair[1]) + "-" + Math.max(pair[0], pair[1]); + uniquePairs.add(pairString); + } + + assertEquals(4, uniquePairs.size()); // Should have 4 unique pairs + } + + // Test case for larger odd-length array + @Test void testPairShuffleLargeOddArray() { + int[] array = {1, 2, 3, 4, 5}; + List pairs = UniquePairShuffle.pairShuffle(array); + + // Result should be empty for odd-length array + assertTrue(pairs.isEmpty()); + } + + // Test case for negative numbers in array + @Test void testPairShuffleNegativeNumbers() { + int[] array = {-1, -2, -3, -4}; + List pairs = UniquePairShuffle.pairShuffle(array); + + // There should be 2 pairs in the result + assertEquals(2, pairs.size()); + + // Check that all pairs are unique + Set uniquePairs = new HashSet<>(); + for (int[] pair : pairs) { + String pairString = + Math.min(pair[0], pair[1]) + "-" + Math.max(pair[0], pair[1]); + uniquePairs.add(pairString); + } + + assertEquals(2, uniquePairs.size()); // Should have 2 unique pairs + } +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/shufflealgo/WeightedShuffleTest.java b/src/test/java/com/thealgorithms/shufflealgo/WeightedShuffleTest.java new file mode 100644 index 000000000000..e0acc6e27966 --- /dev/null +++ b/src/test/java/com/thealgorithms/shufflealgo/WeightedShuffleTest.java @@ -0,0 +1,90 @@ +package com.thealgorithms.shufflealgo; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Assertions; + +import java.util.Arrays; + +public +class WeightedShuffleTest { + + @Test void testWeightedShuffleBasic() { + int[] array = {10, 20, 30}; + int[] weights = {1, 3, 2}; + WeightedShuffle.weightedShuffle(array, weights); + + // Check the array is not in its original order (a simple shuffle check) + Assertions.assertNotEquals(array[0], 10, "Array should be shuffled"); + Assertions.assertNotEquals(Arrays.toString(array), + Arrays.toString(new int[]{10, 20, 30}), + "Array should be shuffled"); + } + + // Test case for empty array + @Test void testWeightedShuffleEmptyArray() { + int[] array = {}; + int[] weights = {}; + WeightedShuffle.weightedShuffle(array, weights); + Assertions.assertArrayEquals(new int[]{}, array); + } + + // Test case for single element array + @Test void testWeightedShuffleSingleElementArray() { + int[] array = {5}; + int[] weights = {10}; + WeightedShuffle.weightedShuffle(array, weights); + Assertions.assertArrayEquals(new int[]{5}, array); + } + + // Test case for multiple elements with same weight + @Test void testWeightedShuffleSameWeights() { + int[] array = {1, 2, 3, 4}; + int[] weights = {1, 1, 1, 1}; // All elements have the same weight + WeightedShuffle.weightedShuffle(array, weights); + // The order should remain the same or be any permutation since weights are + // equal + boolean firstElementMatches = + array[0] == 1 || array[0] == 2 || array[0] == 3 || array[0] == 4; + Assertions.assertTrue(firstElementMatches); + } + + // Test case for null array + @Test void testWeightedShuffleNullArray() { + int[] weights = {1, 2, 3}; + WeightedShuffle.weightedShuffle(null, weights); + // Should not throw any exception, but we can't assert anything since input + // is null + } + + // Test case for null weights + @Test void testWeightedShuffleNullWeights() { + int[] array = {1, 2, 3}; + WeightedShuffle.weightedShuffle(array, null); + // Should not throw any exception and array should remain unchanged + Assertions.assertArrayEquals(new int[]{1, 2, 3}, array); + } + + // Test case for different lengths of array and weights + @Test void testWeightedShuffleDifferentLengths() { + int[] array = {1, 2, 3}; + int[] weights = {1, 2}; // One less weight + WeightedShuffle.weightedShuffle(array, weights); + // Should not throw any exception, but we can't assert anything since input + // is invalid + Assertions.assertArrayEquals( + new int[]{1, 2, 3}, + array); // Original array should remain unchanged + } + + // Test case for all elements with zero weight + @Test void testWeightedShuffleZeroWeights() { + int[] array = {5, 10, 15}; + int[] weights = {0, 0, 0}; // All weights are zero + WeightedShuffle.weightedShuffle(array, weights); + // The order should remain the same or be any permutation since all weights + // are equal + boolean firstElementMatches = + array[0] == 5 || array[0] == 10 || array[0] == 15; + Assertions.assertTrue(firstElementMatches); + } +}