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;
|
package com.thealgorithms.sorts;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
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,
|
* Sorts the given array using the pigeonhole sort algorithm.
|
||||||
but we can also implement this for string arrays too.
|
*
|
||||||
See https://www.geeksforgeeks.org/pigeonhole-sort/
|
* @param array the array to be sorted
|
||||||
*/
|
* @throws IllegalArgumentException if any negative integers are found
|
||||||
void sort(Integer[] array) {
|
* @return the sorted array
|
||||||
int maxElement = array[0];
|
*/
|
||||||
for (int element : array) {
|
public static int[] sort(int[] array) {
|
||||||
if (element > maxElement) {
|
|
||||||
maxElement = element;
|
checkForNegativeInput(array);
|
||||||
}
|
|
||||||
|
if (array.length == 0) {
|
||||||
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
int numOfPigeonholes = 1 + maxElement;
|
final int maxElement = Arrays.stream(array).max().orElseThrow();
|
||||||
ArrayList<Integer>[] pigeonHole = new ArrayList[numOfPigeonholes];
|
final List<List<Integer>> pigeonHoles = createPigeonHoles(maxElement);
|
||||||
|
|
||||||
for (int k = 0; k < numOfPigeonholes; k++) {
|
populatePigeonHoles(array, pigeonHoles);
|
||||||
pigeonHole[k] = new ArrayList<>();
|
collectFromPigeonHoles(array, pigeonHoles);
|
||||||
}
|
|
||||||
|
|
||||||
for (int t : array) {
|
return array;
|
||||||
pigeonHole[t].add(t);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
int k = 0;
|
/**
|
||||||
for (ArrayList<Integer> ph : pigeonHole) {
|
* Checks if the array contains any negative integers.
|
||||||
for (int elements : ph) {
|
*
|
||||||
array[k] = elements;
|
* @param array the array to be checked
|
||||||
k = k + 1;
|
* @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();
|
* Creates pigeonholes for sorting using an ArrayList of ArrayLists.
|
||||||
Integer[] arr = {8, 3, 2, 7, 4, 6, 8};
|
*
|
||||||
|
* @param maxElement the maximum element in the array
|
||||||
System.out.print("Unsorted order is : ");
|
* @return an ArrayList of ArrayLists
|
||||||
SortUtils.print(arr);
|
*/
|
||||||
|
private static List<List<Integer>> createPigeonHoles(int maxElement) {
|
||||||
pigeonholeSort.sort(arr);
|
List<List<Integer>> pigeonHoles = new ArrayList<>(maxElement + 1);
|
||||||
|
for (int i = 0; i <= maxElement; i++) {
|
||||||
System.out.print("Sorted order is : ");
|
pigeonHoles.add(new ArrayList<>());
|
||||||
for (int i = 0; i < arr.length; i++) {
|
}
|
||||||
assert (arr[i]) <= (arr[i + 1]);
|
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