From fd5905e13a93b72749064927a53f27094a255c3e Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Sun, 21 Jul 2024 18:39:34 +0200 Subject: [PATCH 1/4] refactor: PigeonholeSort --- .../thealgorithms/sorts/PigeonholeSort.java | 103 ++++++++++++------ .../sorts/PigeonholeSortTest.java | 39 +++++++ 2 files changed, 107 insertions(+), 35 deletions(-) create mode 100644 src/test/java/com/thealgorithms/sorts/PigeonholeSortTest.java diff --git a/src/main/java/com/thealgorithms/sorts/PigeonholeSort.java b/src/main/java/com/thealgorithms/sorts/PigeonholeSort.java index 42fd026b117b..36bb977bd514 100644 --- a/src/main/java/com/thealgorithms/sorts/PigeonholeSort.java +++ b/src/main/java/com/thealgorithms/sorts/PigeonholeSort.java @@ -1,55 +1,88 @@ package com.thealgorithms.sorts; import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; public 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) { + if (array.length < 2) { + return array; } - int numOfPigeonholes = 1 + maxElement; - ArrayList[] pigeonHole = new ArrayList[numOfPigeonholes]; + checkForNegativeInput(array); - for (int k = 0; k < numOfPigeonholes; k++) { - pigeonHole[k] = new ArrayList<>(); - } + final int maxElement = Arrays.stream(array).max().orElseThrow(); + final List> pigeonHoles = createPigeonHoles(maxElement); - for (int t : array) { - pigeonHole[t].add(t); - } + populatePigeonHoles(array, pigeonHoles); + collectFromPigeonHoles(array, pigeonHoles); + + return array; + } - int k = 0; - for (ArrayList 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 (int number : array) { + if (number < 0) { + throw new IllegalArgumentException("Array contains non-positive 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> createPigeonHoles(int maxElement) { + List> 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> 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> pigeonHoles) { + int index = 0; + for (List pigeonHole : pigeonHoles) { + for (int element : pigeonHole) { + array[index++] = element; + } } - SortUtils.print(arr); } } diff --git a/src/test/java/com/thealgorithms/sorts/PigeonholeSortTest.java b/src/test/java/com/thealgorithms/sorts/PigeonholeSortTest.java new file mode 100644 index 000000000000..342ae349c543 --- /dev/null +++ b/src/test/java/com/thealgorithms/sorts/PigeonholeSortTest.java @@ -0,0 +1,39 @@ +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 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}); + }); + } +} From 59a671b9f96c69fce26142c3adc5c01f9151c44c Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Sun, 21 Jul 2024 18:48:03 +0200 Subject: [PATCH 2/4] checkstyle: fix formatting --- .../thealgorithms/sorts/PigeonholeSortTest.java | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/test/java/com/thealgorithms/sorts/PigeonholeSortTest.java b/src/test/java/com/thealgorithms/sorts/PigeonholeSortTest.java index 342ae349c543..f6ef1022098c 100644 --- a/src/test/java/com/thealgorithms/sorts/PigeonholeSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/PigeonholeSortTest.java @@ -19,21 +19,12 @@ public void testPigeonholeSort(int[] inputArray, int[] expectedArray) { } private static Stream 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}) - ); + 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}); - }); + assertThrows(IllegalArgumentException.class, () -> PigeonholeSort.sort(new int[] {3, 1, 4, 1, 5, -9})); } } From ca598758923f5ea0e3876b7bf7887a6bf2999f87 Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Sun, 21 Jul 2024 19:01:57 +0200 Subject: [PATCH 3/4] checkstyle: make class final --- src/main/java/com/thealgorithms/sorts/PigeonholeSort.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/sorts/PigeonholeSort.java b/src/main/java/com/thealgorithms/sorts/PigeonholeSort.java index 36bb977bd514..4a47512572a3 100644 --- a/src/main/java/com/thealgorithms/sorts/PigeonholeSort.java +++ b/src/main/java/com/thealgorithms/sorts/PigeonholeSort.java @@ -4,7 +4,7 @@ import java.util.Arrays; import java.util.List; -public class PigeonholeSort { +public final class PigeonholeSort { private PigeonholeSort() { } From 7d3fc42f571fe63f0f06d88ca85a4e96df644f91 Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Thu, 25 Jul 2024 09:54:09 +0200 Subject: [PATCH 4/4] refactor: changing negative numbers check first, fix typo, adding one more test for negative numbers --- .../com/thealgorithms/sorts/PigeonholeSort.java | 15 ++++++++------- .../thealgorithms/sorts/PigeonholeSortTest.java | 1 + 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/PigeonholeSort.java b/src/main/java/com/thealgorithms/sorts/PigeonholeSort.java index 4a47512572a3..78d7d81d709f 100644 --- a/src/main/java/com/thealgorithms/sorts/PigeonholeSort.java +++ b/src/main/java/com/thealgorithms/sorts/PigeonholeSort.java @@ -16,12 +16,13 @@ private PigeonholeSort() { * @return the sorted array */ public static int[] sort(int[] array) { - if (array.length < 2) { - return array; - } checkForNegativeInput(array); + if (array.length == 0) { + return array; + } + final int maxElement = Arrays.stream(array).max().orElseThrow(); final List> pigeonHoles = createPigeonHoles(maxElement); @@ -38,9 +39,9 @@ public static int[] sort(int[] array) { * @throws IllegalArgumentException if any negative integers are found */ private static void checkForNegativeInput(int[] array) { - for (int number : array) { + for (final int number : array) { if (number < 0) { - throw new IllegalArgumentException("Array contains non-positive integers."); + throw new IllegalArgumentException("Array contains negative integers."); } } } @@ -79,8 +80,8 @@ private static void populatePigeonHoles(int[] array, List> pigeonH */ private static void collectFromPigeonHoles(int[] array, List> pigeonHoles) { int index = 0; - for (List pigeonHole : pigeonHoles) { - for (int element : pigeonHole) { + for (final var pigeonHole : pigeonHoles) { + for (final int element : pigeonHole) { array[index++] = element; } } diff --git a/src/test/java/com/thealgorithms/sorts/PigeonholeSortTest.java b/src/test/java/com/thealgorithms/sorts/PigeonholeSortTest.java index f6ef1022098c..d1772de83701 100644 --- a/src/test/java/com/thealgorithms/sorts/PigeonholeSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/PigeonholeSortTest.java @@ -26,5 +26,6 @@ private static Stream provideArraysForPigeonholeSort() { @Test public void testWithNegativeNumbers() { assertThrows(IllegalArgumentException.class, () -> PigeonholeSort.sort(new int[] {3, 1, 4, 1, 5, -9})); + assertThrows(IllegalArgumentException.class, () -> PigeonholeSort.sort(new int[] {-1})); } }