-
Notifications
You must be signed in to change notification settings - Fork 20k
Added Pigeonhole Sort with its corresponding test cases #670
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package src.main.java.com.sorts; | ||
|
||
public class PigeonholeSort { | ||
|
||
/** | ||
* This method sorts the array using Pigeonhole sort technique. | ||
* <p> | ||
* 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. | ||
* <p> | ||
* 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 = Integer.MAX_VALUE; | ||
int max = Integer.MIN_VALUE; | ||
|
||
for (Integer integer : arr) { | ||
min = Math.min(min, integer); | ||
max = Math.max(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) { | ||
// This increment operation will count for the duplicates elements, if present | ||
pigeonholes[integer - min]++; | ||
} | ||
|
||
// Index for the original array | ||
int index = 0; | ||
|
||
// Loop over pigeonhole array | ||
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; | ||
} | ||
} | ||
|
||
return arr; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/test/java/src/test/java/com/sorts/PigeonholeSortTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
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)); | ||
|
||
// 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)); | ||
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.