forked from williamfiset/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountingSort.java
44 lines (39 loc) · 1.3 KB
/
CountingSort.java
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
/**
* An implementation of counting sort!
*
* <p>Run with:
*
* <p>$ ./gradlew run -Palgorithm=sorting.CountingSort
*
* @author William Fiset, [email protected]
*/
package com.williamfiset.algorithms.sorting;
public class CountingSort implements InplaceSort {
@Override
public void sort(int[] values) {
int minValue = Integer.MAX_VALUE;
int maxValue = Integer.MIN_VALUE;
for (int i = 0; i < values.length; i++) {
if (values[i] < minValue) minValue = values[i];
if (values[i] > maxValue) maxValue = values[i];
}
CountingSort.countingSort(values, minValue, maxValue);
}
// Sorts values in the range of [minVal, maxVal] in O(n+maxVal-maxVal)
private static void countingSort(int[] ar, int minVal, int maxVal) {
int sz = maxVal - minVal + 1;
int[] b = new int[sz];
for (int i = 0; i < ar.length; i++) b[ar[i] - minVal]++;
for (int i = 0, k = 0; i < sz; i++) {
while (b[i]-- > 0) ar[k++] = i + minVal;
}
}
public static void main(String[] args) {
CountingSort sorter = new CountingSort();
int[] nums = {+4, -10, +0, +6, +1, -5, -5, +1, +1, -2, 0, +6, +8, -7, +10};
sorter.sort(nums);
// Prints:
// [-10, -7, -5, -5, -2, 0, 0, 1, 1, 1, 4, 6, 6, 8, 10]
System.out.println(java.util.Arrays.toString(nums));
}
}