-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountingSort.java
More file actions
52 lines (39 loc) · 1.55 KB
/
CountingSort.java
File metadata and controls
52 lines (39 loc) · 1.55 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
public class CountingSort {
public static int[] countingSort(int[] arr) {
int aLength = arr.length;
// 1. find highest value
int maxValue = arr[0];
for (int i = 1; i <= aLength - 1; i++) {
if (arr[i] > maxValue) maxValue = arr[i];
}
// 2. create counting array; + 1 because array starts at index 0
int[] c = new int[maxValue + 1];
// 3. for every number in the unsorted array, increase the value in the c array at that index for 1
for (int i = 0; i <= aLength - 1; i++) {
c[arr[i]]++;
}
// 4. summ up the values in the c array
// index 0,1,2 -> 0,1,2
// value 1,2,1 -> 1,3,4
for (int i = 1; i <= c.length - 1; i++) {
c[i] = c[i] + c[i - 1];
}
// print out c array
System.out.print("c array: ");
for (int i = 0; i <= c.length - 1; i++) {
System.out.print(c[i] + ", ");
}
System.out.println();
// 5. create the sorted array
int[] sorted = new int[aLength];
// 6. add the numbers in rising order into the sorted array
for (int i = aLength - 1; i >= 0; i--) {
System.out.println("index: " + i + ", position: " + c[arr[i]] + ", value: " + arr[i]);
// -1 neccessary, because array index starts at 0
sorted[c[arr[i]] - 1] = arr[i];
// reduce value by one, so that if the same value appears again, it will be put infront
c[arr[i]]--;
}
return sorted;
}
}