-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountingSort.java
More file actions
35 lines (29 loc) · 861 Bytes
/
CountingSort.java
File metadata and controls
35 lines (29 loc) · 861 Bytes
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
package algorithms.sort;
/**
* UnStable sort implementation
* time: O(n) Linear Time
* Not in-place Algorithm
* use only when the range of values is reasonable
*/
public class CountingSort{
public static void countingSort(int[] intArray, int min, int max){
int[] countArray = new int[(max - min) + 1];
for (int k : intArray) {
countArray[k - min]++;
}
int j = 0;
for(int i = min; i <= max; i++) {
while(countArray[i - min] > 0) {
intArray[j++] = i;
countArray[i - min]--;
}
}
}
public static void main(String[] args) {
int[] intArray = {1, 1, 4, 6, 2, 6, 3, 7, 7, 10, 34, 6};
countingSort(intArray, 1, 34);
for (int item : intArray) {
System.out.println(item);
}
}
}