From 005380f538e930bd8d45197f447ccfba0042a9e8 Mon Sep 17 00:00:00 2001 From: Anirudh Sharma Date: Fri, 4 Jan 2019 12:29:42 +0530 Subject: [PATCH 1/3] Added Pigeonhole Sort with its corresponding test cases --- .../main/java/com/sorts/PigeonholeSort.java | 61 +++++++++++++++++++ .../java/com/sorts/PigeonholeSortTest.java | 25 ++++++++ 2 files changed, 86 insertions(+) create mode 100644 src/main/java/src/main/java/com/sorts/PigeonholeSort.java create mode 100644 src/test/java/src/test/java/com/sorts/PigeonholeSortTest.java diff --git a/src/main/java/src/main/java/com/sorts/PigeonholeSort.java b/src/main/java/src/main/java/com/sorts/PigeonholeSort.java new file mode 100644 index 00000000..2d119386 --- /dev/null +++ b/src/main/java/src/main/java/com/sorts/PigeonholeSort.java @@ -0,0 +1,61 @@ +package src.main.java.com.sorts; + +public class PigeonholeSort { + + /** + * This method sorts the array using Pigeonhole sort technique. + *

+ * Pigeonhole sorting is a sorting algorithms that is suitable for sorting lists of elements where the number + * of elements and the number of possible key values are approximately the same. + *

+ * It requires O(n + Range) time where n is number of elements in input array and ‘Range’ is number of possible + * values in array. + * + * @param arr The array to be sorted + * @return arr Sorted array + */ + public Integer[] sort(Integer[] arr) { + + // Find maximum and minimum elements in array + int min = arr[0]; + int max = arr[0]; + + for (Integer integer : arr) { + + // For minimum value + if (min > integer) { + min = integer; + } + + // For maximum value + if (max < integer) { + max = integer; + } + } + + // Range + int range = max - min + 1; + + // Pigeonhole array + int[] pigeonholes = new int[range]; + + // Put each element of arr in its pigeonhole + for (Integer integer : arr) { + pigeonholes[integer - min] = integer; + } + + // Index for the arr + int index = 0; + + // Loop over pigeonhole array + for (int pigeonhole : pigeonholes) { + + // Put non zero elements from the pigeonhole array to the current element of arr + if (pigeonhole != 0) { + arr[index++] = pigeonhole; + } + } + + return arr; + } +} diff --git a/src/test/java/src/test/java/com/sorts/PigeonholeSortTest.java b/src/test/java/src/test/java/com/sorts/PigeonholeSortTest.java new file mode 100644 index 00000000..cc163534 --- /dev/null +++ b/src/test/java/src/test/java/com/sorts/PigeonholeSortTest.java @@ -0,0 +1,25 @@ +package src.test.java.com.sorts; + +import org.junit.Assert; +import org.junit.Test; +import src.main.java.com.sorts.PigeonholeSort; + +public class PigeonholeSortTest { + + @Test + public void testPigeonholeSort() { + + PigeonholeSort pigeonholeSort = new PigeonholeSort(); + + // Test Case 1 + Integer[] unsorted1 = new Integer[]{5, 1, 7, 2, 9, 6, 3, 4, 8}; + Integer[] sorted1 = new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9}; + Assert.assertArrayEquals(sorted1, pigeonholeSort.sort(unsorted1)); + + // Test Case 2 + Integer[] unsorted2 = new Integer[]{-5, 1, 7, 2, -9, 6, -3, 4, 8}; + Integer[] sorted2 = new Integer[]{-9, -5, -3, 1, 2, 4, 6, 7, 8}; + Assert.assertArrayEquals(sorted2, pigeonholeSort.sort(unsorted2)); + + } +} From 9fe55c82d8aec9cbae37c7599c582aa88a43434b Mon Sep 17 00:00:00 2001 From: Anirudh Sharma Date: Fri, 4 Jan 2019 17:59:20 +0530 Subject: [PATCH 2/3] Added Pigeonhole Sort with its corresponding test cases --- .../src/main/java/com/sorts/PigeonholeSort.java | 17 ++++++++++------- .../test/java/com/sorts/PigeonholeSortTest.java | 5 +++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/main/java/src/main/java/com/sorts/PigeonholeSort.java b/src/main/java/src/main/java/com/sorts/PigeonholeSort.java index 2d119386..6b77050b 100644 --- a/src/main/java/src/main/java/com/sorts/PigeonholeSort.java +++ b/src/main/java/src/main/java/com/sorts/PigeonholeSort.java @@ -41,18 +41,21 @@ public class PigeonholeSort { // Put each element of arr in its pigeonhole for (Integer integer : arr) { - pigeonholes[integer - min] = integer; + // This increment operation will count for the duplicates elements, if present + pigeonholes[integer - min]++; } - // Index for the arr + // Index for the original array int index = 0; // Loop over pigeonhole array - for (int pigeonhole : pigeonholes) { - - // Put non zero elements from the pigeonhole array to the current element of arr - if (pigeonhole != 0) { - arr[index++] = pigeonhole; + for (int i = 0; i < range; i++) { + // This inner loop will execute only for those indexes in + // pigeonhole which are greater than zero i.e., only for those + // elements which are present in the original array. This also + // takes care of the duplicate elements + while (pigeonholes[i]-- > 0) { + arr[index++] = i + min; } } diff --git a/src/test/java/src/test/java/com/sorts/PigeonholeSortTest.java b/src/test/java/src/test/java/com/sorts/PigeonholeSortTest.java index cc163534..5195fc51 100644 --- a/src/test/java/src/test/java/com/sorts/PigeonholeSortTest.java +++ b/src/test/java/src/test/java/com/sorts/PigeonholeSortTest.java @@ -21,5 +21,10 @@ public class PigeonholeSortTest { Integer[] sorted2 = new Integer[]{-9, -5, -3, 1, 2, 4, 6, 7, 8}; Assert.assertArrayEquals(sorted2, pigeonholeSort.sort(unsorted2)); + // Test Case 3 + Integer[] unsorted3 = new Integer[]{-5, 1, 7, 2, -9, 6, -3, 4, 1, 8, 1, 1}; + Integer[] sorted3 = new Integer[]{-9, -5, -3, 1, 1, 1, 1, 2, 4, 6, 7, 8}; + Assert.assertArrayEquals(sorted3, pigeonholeSort.sort(unsorted3)); + } } From 1274140ea369fcf1a806bec99a495cb93220bf4a Mon Sep 17 00:00:00 2001 From: Libin Yang Date: Fri, 4 Jan 2019 20:38:49 +0800 Subject: [PATCH 3/3] Update PigeonholeSort.java --- .../src/main/java/com/sorts/PigeonholeSort.java | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/main/java/src/main/java/com/sorts/PigeonholeSort.java b/src/main/java/src/main/java/com/sorts/PigeonholeSort.java index 6b77050b..1f8b498b 100644 --- a/src/main/java/src/main/java/com/sorts/PigeonholeSort.java +++ b/src/main/java/src/main/java/com/sorts/PigeonholeSort.java @@ -17,20 +17,12 @@ public class PigeonholeSort { public Integer[] sort(Integer[] arr) { // Find maximum and minimum elements in array - int min = arr[0]; - int max = arr[0]; + int min = Integer.MAX_VALUE; + int max = Integer.MIN_VALUE; for (Integer integer : arr) { - - // For minimum value - if (min > integer) { - min = integer; - } - - // For maximum value - if (max < integer) { - max = integer; - } + min = Math.min(min, integer); + max = Math.max(max, integer); } // Range