Added ShuffleAlgorithm and various problems related to it. #6053
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Shuffle Algorithms for Various Scenarios
This PR introduces multiple shuffle algorithms to the com.thealgorithms.shufflealgorithm package, with each class implementing a specific type of shuffle technique. The classes are designed to be flexible for different shuffling scenarios with well-documented methods and edge case handling. Below is a summary of the classes and the shuffle techniques they provide:
Purpose: Shuffle the elements in an array while keeping the first and last elements fixed.
Method: constrainedShuffle(int[] array)
Edge Case Handling: No shuffling occurs if the array has fewer than 3 elements.
Purpose: Groups elements into subarrays of a specified size and shuffles those groups.
Method: groupShuffle(int[] array, int groupSize) -> List<List>
Edge Case Handling: Ensures that each group has the specified size (or fewer for the last group) and skips shuffling if groupSize <= 1.
Purpose: Shuffle elements within a specified index range.
Method: shuffleByRange(int[] array, int start, int end)
Edge Case Handling: Verifies valid indices to avoid IndexOutOfBounds issues.
Purpose: Basic shuffle algorithm that uses a uniform probability approach to randomize array elements.
Method: shuffle(int[] array)
Purpose: Pairs elements randomly without repeating pairs.
Method: pairShuffle(int[] array) -> List<int[]>
Edge Case Handling: Returns an empty list if the array has an odd length or fewer than 2 elements.
Purpose: Shuffles elements based on their weights, favoring elements with higher weights.
Method: weightedShuffle(int[] array, int[] weights)
Edge Case Handling: Ensures the weights array matches the array length for proper assignment