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 <alx@alx.com> Co-authored-by: vil02 <65706193+vil02@users.noreply.github.com>
This commit is contained in:
parent
76a450fb75
commit
ebed8b38b8
@ -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) {
|
||||
|
||||
checkForNegativeInput(array);
|
||||
|
||||
if (array.length == 0) {
|
||||
return array;
|
||||
}
|
||||
|
||||
int numOfPigeonholes = 1 + maxElement;
|
||||
ArrayList<Integer>[] pigeonHole = new ArrayList[numOfPigeonholes];
|
||||
final int maxElement = Arrays.stream(array).max().orElseThrow();
|
||||
final List<List<Integer>> pigeonHoles = createPigeonHoles(maxElement);
|
||||
|
||||
for (int k = 0; k < numOfPigeonholes; k++) {
|
||||
pigeonHole[k] = new ArrayList<>();
|
||||
}
|
||||
populatePigeonHoles(array, pigeonHoles);
|
||||
collectFromPigeonHoles(array, pigeonHoles);
|
||||
|
||||
for (int t : array) {
|
||||
pigeonHole[t].add(t);
|
||||
}
|
||||
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);
|
||||
|
||||
pigeonholeSort.sort(arr);
|
||||
|
||||
System.out.print("Sorted order is : ");
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
assert (arr[i]) <= (arr[i + 1]);
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
@ -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}));
|
||||
assertThrows(IllegalArgumentException.class, () -> PigeonholeSort.sort(new int[] {-1}));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user