-
Notifications
You must be signed in to change notification settings - Fork 21k
refactor: cleanup PigeonholeSort
#5298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fd5905e
refactor: PigeonholeSort
59a671b
checkstyle: fix formatting
ca59875
checkstyle: make class final
bc2ad8f
Merge branch 'master' into refactor/pigeonhole_sort
alxkm a19e8d1
Merge branch 'master' into refactor/pigeonhole_sort
alxkm 7d3fc42
refactor: changing negative numbers check first, fix typo, adding one…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
106 changes: 70 additions & 36 deletions
106
src/main/java/com/thealgorithms/sorts/PigeonholeSort.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,55 +1,89 @@ | ||
| package com.thealgorithms.sorts; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| public class PigeonholeSort { | ||
| public final class PigeonholeSort { | ||
| private PigeonholeSort() { | ||
| } | ||
|
|
||
| /* | ||
| This code implements the pigeonhole sort algorithm for the integer array, | ||
| but we can also implement this for string arrays too. | ||
| See https://www.geeksforgeeks.org/pigeonhole-sort/ | ||
| */ | ||
| void sort(Integer[] array) { | ||
| int maxElement = array[0]; | ||
| for (int element : array) { | ||
| if (element > maxElement) { | ||
| maxElement = element; | ||
| } | ||
| } | ||
| /** | ||
| * Sorts the given array using the pigeonhole sort algorithm. | ||
| * | ||
| * @param array the array to be sorted | ||
| * @throws IllegalArgumentException if any negative integers are found | ||
| * @return the sorted array | ||
| */ | ||
| public static int[] sort(int[] array) { | ||
|
|
||
| int numOfPigeonholes = 1 + maxElement; | ||
| ArrayList<Integer>[] pigeonHole = new ArrayList[numOfPigeonholes]; | ||
| checkForNegativeInput(array); | ||
|
|
||
| for (int k = 0; k < numOfPigeonholes; k++) { | ||
| pigeonHole[k] = new ArrayList<>(); | ||
| if (array.length == 0) { | ||
| return array; | ||
| } | ||
|
|
||
| for (int t : array) { | ||
| pigeonHole[t].add(t); | ||
| } | ||
| final int maxElement = Arrays.stream(array).max().orElseThrow(); | ||
| final List<List<Integer>> pigeonHoles = createPigeonHoles(maxElement); | ||
|
|
||
| populatePigeonHoles(array, pigeonHoles); | ||
| collectFromPigeonHoles(array, pigeonHoles); | ||
|
|
||
| return array; | ||
| } | ||
|
|
||
| int k = 0; | ||
| for (ArrayList<Integer> ph : pigeonHole) { | ||
| for (int elements : ph) { | ||
| array[k] = elements; | ||
| k = k + 1; | ||
| /** | ||
| * Checks if the array contains any negative integers. | ||
| * | ||
| * @param array the array to be checked | ||
| * @throws IllegalArgumentException if any negative integers are found | ||
| */ | ||
| private static void checkForNegativeInput(int[] array) { | ||
| for (final int number : array) { | ||
| if (number < 0) { | ||
| throw new IllegalArgumentException("Array contains negative integers."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| PigeonholeSort pigeonholeSort = new PigeonholeSort(); | ||
| Integer[] arr = {8, 3, 2, 7, 4, 6, 8}; | ||
|
|
||
| System.out.print("Unsorted order is : "); | ||
| SortUtils.print(arr); | ||
| /** | ||
| * Creates pigeonholes for sorting using an ArrayList of ArrayLists. | ||
| * | ||
| * @param maxElement the maximum element in the array | ||
| * @return an ArrayList of ArrayLists | ||
| */ | ||
| private static List<List<Integer>> createPigeonHoles(int maxElement) { | ||
| List<List<Integer>> pigeonHoles = new ArrayList<>(maxElement + 1); | ||
| for (int i = 0; i <= maxElement; i++) { | ||
| pigeonHoles.add(new ArrayList<>()); | ||
| } | ||
| return pigeonHoles; | ||
| } | ||
|
|
||
| pigeonholeSort.sort(arr); | ||
| /** | ||
| * Populates the pigeonholes with elements from the array. | ||
| * | ||
| * @param array the array to be sorted | ||
| * @param pigeonHoles the pigeonholes to be populated | ||
| */ | ||
| private static void populatePigeonHoles(int[] array, List<List<Integer>> pigeonHoles) { | ||
| for (int element : array) { | ||
| pigeonHoles.get(element).add(element); | ||
| } | ||
| } | ||
|
|
||
| System.out.print("Sorted order is : "); | ||
| for (int i = 0; i < arr.length; i++) { | ||
| assert (arr[i]) <= (arr[i + 1]); | ||
| /** | ||
| * Collects sorted elements from the pigeonholes back into the array. | ||
| * | ||
| * @param array the array to be sorted | ||
| * @param pigeonHoles the populated pigeonholes | ||
| */ | ||
| private static void collectFromPigeonHoles(int[] array, List<List<Integer>> pigeonHoles) { | ||
| int index = 0; | ||
| for (final var pigeonHole : pigeonHoles) { | ||
| for (final int element : pigeonHole) { | ||
| array[index++] = element; | ||
| } | ||
| } | ||
| SortUtils.print(arr); | ||
| } | ||
| } | ||
31 changes: 31 additions & 0 deletions
31
src/test/java/com/thealgorithms/sorts/PigeonholeSortTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.thealgorithms.sorts; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import java.util.stream.Stream; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.Arguments; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
|
|
||
| public class PigeonholeSortTest { | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("provideArraysForPigeonholeSort") | ||
| public void testPigeonholeSort(int[] inputArray, int[] expectedArray) { | ||
| PigeonholeSort.sort(inputArray); | ||
| assertArrayEquals(expectedArray, inputArray); | ||
| } | ||
|
|
||
| private static Stream<Arguments> provideArraysForPigeonholeSort() { | ||
| return Stream.of(Arguments.of(new int[] {}, new int[] {}), Arguments.of(new int[] {4}, new int[] {4}), Arguments.of(new int[] {6, 1, 99, 27, 15, 23, 36}, new int[] {1, 6, 15, 23, 27, 36, 99}), Arguments.of(new int[] {6, 1, 27, 15, 23, 27, 36, 23}, new int[] {1, 6, 15, 23, 23, 27, 27, 36}), | ||
| Arguments.of(new int[] {5, 5, 5, 5, 5}, new int[] {5, 5, 5, 5, 5}), Arguments.of(new int[] {1, 2, 3, 4, 5}, new int[] {1, 2, 3, 4, 5}), Arguments.of(new int[] {5, 4, 3, 2, 1}, new int[] {1, 2, 3, 4, 5})); | ||
| } | ||
|
|
||
| @Test | ||
| public void testWithNegativeNumbers() { | ||
| assertThrows(IllegalArgumentException.class, () -> PigeonholeSort.sort(new int[] {3, 1, 4, 1, 5, -9})); | ||
alxkm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assertThrows(IllegalArgumentException.class, () -> PigeonholeSort.sort(new int[] {-1})); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.