Skip to content

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 6 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 69 additions & 36 deletions src/main/java/com/thealgorithms/sorts/PigeonholeSort.java
Original file line number Diff line number Diff line change
@@ -1,55 +1,88 @@
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) {
if (array.length < 2) {
return array;
}

int numOfPigeonholes = 1 + maxElement;
ArrayList<Integer>[] 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<List<Integer>> pigeonHoles = createPigeonHoles(maxElement);

for (int t : array) {
pigeonHole[t].add(t);
}
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 (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<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 (List<Integer> pigeonHole : pigeonHoles) {
for (int element : pigeonHole) {
array[index++] = element;
}
}
SortUtils.print(arr);
}
}
30 changes: 30 additions & 0 deletions src/test/java/com/thealgorithms/sorts/PigeonholeSortTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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}));
}
}