-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountingSort.java
More file actions
42 lines (36 loc) · 1.13 KB
/
CountingSort.java
File metadata and controls
42 lines (36 loc) · 1.13 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
public class CountingSort {
public static void main(String[] args) {
int a[] = { 3, 6, 4, 1, 3, 4, 1, 4, 2 };
countingSort(a);
// Optional: Print the sorted array
for (int num : a) {
System.out.print(num + " ");
}
}
public static void countingSort(int a[]) {
// Find the maximum value in the array
int max = a[0];
for (int i = 1; i < a.length; i++) {
if (a[i] > max) {
max = a[i];
}
}
// Initialize the count array
int[] demo = new int[max + 1];
for (int i = 0; i < a.length; i++) {
demo[a[i]]++;
}
// Update count array with cumulative counts
for (int i = 1; i < demo.length; i++) {
demo[i] += demo[i - 1];
}
// Build the output array
int[] output = new int[a.length];
for (int i = a.length - 1; i >= 0; i--) {
output[demo[a[i]] - 1] = a[i];
demo[a[i]]--;
}
// Copy the sorted array back to the original array
System.arraycopy(output, 0, a, 0, a.length);
}
}