|
1 | 1 | package com.thealgorithms.sorts; |
2 | 2 |
|
3 | 3 | import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.List; |
4 | 6 |
|
5 | | -public class PigeonholeSort { |
| 7 | +public final class PigeonholeSort { |
| 8 | + private PigeonholeSort() { |
| 9 | + } |
6 | 10 |
|
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) { |
19 | 19 |
|
20 | | - int numOfPigeonholes = 1 + maxElement; |
21 | | - ArrayList<Integer>[] pigeonHole = new ArrayList[numOfPigeonholes]; |
| 20 | + checkForNegativeInput(array); |
22 | 21 |
|
23 | | - for (int k = 0; k < numOfPigeonholes; k++) { |
24 | | - pigeonHole[k] = new ArrayList<>(); |
| 22 | + if (array.length == 0) { |
| 23 | + return array; |
25 | 24 | } |
26 | 25 |
|
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 | + } |
30 | 34 |
|
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."); |
36 | 45 | } |
37 | 46 | } |
38 | 47 | } |
39 | 48 |
|
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 | + } |
46 | 62 |
|
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 | + } |
48 | 74 |
|
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 | + } |
52 | 87 | } |
53 | | - SortUtils.print(arr); |
54 | 88 | } |
55 | 89 | } |
0 commit comments