Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
56 lines (46 loc) · 1.76 KB

File metadata and controls

56 lines (46 loc) · 1.76 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package 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;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.