-
-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathCountingSort.java
More file actions
44 lines (33 loc) · 1.04 KB
/
Copy pathCountingSort.java
File metadata and controls
44 lines (33 loc) · 1.04 KB
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
package util;
import java.util.Arrays;
/**
* Created by nikoo28 on 12/26/20 8:16 PM
*/
public class CountingSort {
static void countingSort(int[] arr)
{
int max = Arrays.stream(arr).max().getAsInt();
int min = Arrays.stream(arr).min().getAsInt();
int range = max - min + 1;
// Auxiliary array to store counts of each element
int[] count = new int[range];
// Auxiliary array to store the resultant sorted array
int[] output = new int[arr.length];
// Store count of each element
for (int j : arr) {
count[j - min]++;
}
// Change count[i] so that count[i] now contains actual
// position of this character in output array
for (int i = 1; i < count.length; i++) {
count[i] += count[i - 1];
}
// Build the output character array
// To make it stable we are operating in reverse order.
for (int i = arr.length - 1; i >= 0; i--) {
output[count[arr[i] - min] - 1] = arr[i];
count[arr[i] - min]--;
}
System.arraycopy(output, 0, arr, 0, arr.length);
}
}