Skip to content

Commit ebed8b3

Browse files
alxkmAlex Klymenkovil02
authored
refactor: cleanup PigeonholeSort (#5298)
* refactor: PigeonholeSort * checkstyle: fix formatting * checkstyle: make class final * refactor: changing negative numbers check first, fix typo, adding one more test for negative numbers --------- Co-authored-by: Alex Klymenko <[email protected]> Co-authored-by: vil02 <[email protected]>
1 parent 76a450f commit ebed8b3

File tree

2 files changed

+101
-36
lines changed

2 files changed

+101
-36
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,89 @@
11
package com.thealgorithms.sorts;
22

33
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
46

5-
public class PigeonholeSort {
7+
public final class PigeonholeSort {
8+
private PigeonholeSort() {
9+
}
610

7-
/*
8-
This code implements the pigeonhole sort algorithm for the integer array,
9-
but we can also implement this for string arrays too.
10-
See https://www.geeksforgeeks.org/pigeonhole-sort/
11-
*/
12-
void sort(Integer[] array) {
13-
int maxElement = array[0];
14-
for (int element : array) {
15-
if (element > maxElement) {
16-
maxElement = element;
17-
}
18-
}
11+
/**
12+
* Sorts the given array using the pigeonhole sort algorithm.
13+
*
14+
* @param array the array to be sorted
15+
* @throws IllegalArgumentException if any negative integers are found
16+
* @return the sorted array
17+
*/
18+
public static int[] sort(int[] array) {
1919

20-
int numOfPigeonholes = 1 + maxElement;
21-
ArrayList<Integer>[] pigeonHole = new ArrayList[numOfPigeonholes];
20+
checkForNegativeInput(array);
2221

23-
for (int k = 0; k < numOfPigeonholes; k++) {
24-
pigeonHole[k] = new ArrayList<>();
22+
if (array.length == 0) {
23+
return array;
2524
}
2625

27-
for (int t : array) {
28-
pigeonHole[t].add(t);
29-
}
26+
final int maxElement = Arrays.stream(array).max().orElseThrow();
27+
final List<List<Integer>> pigeonHoles = createPigeonHoles(maxElement);
28+
29+
populatePigeonHoles(array, pigeonHoles);
30+
collectFromPigeonHoles(array, pigeonHoles);
31+
32+
return array;
33+
}
3034

31-
int k = 0;
32-
for (ArrayList<Integer> ph : pigeonHole) {
33-
for (int elements : ph) {
34-
array[k] = elements;
35-
k = k + 1;
35+
/**
36+
* Checks if the array contains any negative integers.
37+
*
38+
* @param array the array to be checked
39+
* @throws IllegalArgumentException if any negative integers are found
40+
*/
41+
private static void checkForNegativeInput(int[] array) {
42+
for (final int number : array) {
43+
if (number < 0) {
44+
throw new IllegalArgumentException("Array contains negative integers.");
3645
}
3746
}
3847
}
3948

40-
public static void main(String[] args) {
41-
PigeonholeSort pigeonholeSort = new PigeonholeSort();
42-
Integer[] arr = {8, 3, 2, 7, 4, 6, 8};
43-
44-
System.out.print("Unsorted order is : ");
45-
SortUtils.print(arr);
49+
/**
50+
* Creates pigeonholes for sorting using an ArrayList of ArrayLists.
51+
*
52+
* @param maxElement the maximum element in the array
53+
* @return an ArrayList of ArrayLists
54+
*/
55+
private static List<List<Integer>> createPigeonHoles(int maxElement) {
56+
List<List<Integer>> pigeonHoles = new ArrayList<>(maxElement + 1);
57+
for (int i = 0; i <= maxElement; i++) {
58+
pigeonHoles.add(new ArrayList<>());
59+
}
60+
return pigeonHoles;
61+
}
4662

47-
pigeonholeSort.sort(arr);
63+
/**
64+
* Populates the pigeonholes with elements from the array.
65+
*
66+
* @param array the array to be sorted
67+
* @param pigeonHoles the pigeonholes to be populated
68+
*/
69+
private static void populatePigeonHoles(int[] array, List<List<Integer>> pigeonHoles) {
70+
for (int element : array) {
71+
pigeonHoles.get(element).add(element);
72+
}
73+
}
4874

49-
System.out.print("Sorted order is : ");
50-
for (int i = 0; i < arr.length; i++) {
51-
assert (arr[i]) <= (arr[i + 1]);
75+
/**
76+
* Collects sorted elements from the pigeonholes back into the array.
77+
*
78+
* @param array the array to be sorted
79+
* @param pigeonHoles the populated pigeonholes
80+
*/
81+
private static void collectFromPigeonHoles(int[] array, List<List<Integer>> pigeonHoles) {
82+
int index = 0;
83+
for (final var pigeonHole : pigeonHoles) {
84+
for (final int element : pigeonHole) {
85+
array[index++] = element;
86+
}
5287
}
53-
SortUtils.print(arr);
5488
}
5589
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.thealgorithms.sorts;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.Arguments;
10+
import org.junit.jupiter.params.provider.MethodSource;
11+
12+
public class PigeonholeSortTest {
13+
14+
@ParameterizedTest
15+
@MethodSource("provideArraysForPigeonholeSort")
16+
public void testPigeonholeSort(int[] inputArray, int[] expectedArray) {
17+
PigeonholeSort.sort(inputArray);
18+
assertArrayEquals(expectedArray, inputArray);
19+
}
20+
21+
private static Stream<Arguments> provideArraysForPigeonholeSort() {
22+
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}),
23+
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}));
24+
}
25+
26+
@Test
27+
public void testWithNegativeNumbers() {
28+
assertThrows(IllegalArgumentException.class, () -> PigeonholeSort.sort(new int[] {3, 1, 4, 1, 5, -9}));
29+
assertThrows(IllegalArgumentException.class, () -> PigeonholeSort.sort(new int[] {-1}));
30+
}
31+
}

0 commit comments

Comments
 (0)