-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathCounting-Sort algorithm
More file actions
53 lines (44 loc) · 1.41 KB
/
Counting-Sort algorithm
File metadata and controls
53 lines (44 loc) · 1.41 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
45
46
47
48
49
50
51
52
53
import java.util.Arrays;
class Main
{
/*
`arr` ——> the input integer array to be sorted
`k` ——> a number such that all integers are in range `0…k-1`
*/
public static void countsort(int[] arr, int k)
{
// create an integer array of size `n` to store the sorted array
int[] output = new int[arr.length];
// create an integer array of size `k + 1`, initialized by all zero
int[] freq = new int[k + 1];
// using the value of each item in the input array as an index,
// store each integer's count in `freq[]`
for (int i: arr) {
freq[i]++;
}
// calculate the starting index for each integer
int total = 0;
for (int i = 0; i < k + 1; i++)
{
int oldCount = freq[i];
freq[i] = total;
total += oldCount;
}
// copy to the output array, preserving the order of inputs with equal keys
for (int i: arr)
{
output[freq[i]] = i;
freq[i]++;
}
// copy the output array back to the input array
Arrays.setAll(arr, i -> output[i]);
}
public static void main(String[] args)
{
int[] arr = { 4, 2, 10, 10, 1, 4, 2, 1, 10 };
// range of array elements
int k = 10;
countsort(arr, k);
System.out.println(Arrays.toString(arr));
}
}