-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountingSort.java
More file actions
35 lines (32 loc) · 926 Bytes
/
CountingSort.java
File metadata and controls
35 lines (32 loc) · 926 Bytes
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
package Sorting;
public class CountingSort {
public static void countSort(int[] array, int k) {
int n = array.length;
int[] temp = new int[n];
int[] counts = new int[k + 1];
for (int i = 0; i < n; i++) {
counts[array[i]]++;
}
for (int i = 1; i <= k; i++) {
counts[i] += counts[i - 1];
}
for (int i = n - 1; i >= 0; i--) {
temp[counts[array[i]] - 1] = array[i];
counts[array[i]]--;
}
for (int i = 0; i < n; i++) {
array[i] = temp[i];
}
}
public static void main(String[] args) {
int[] numbers = {3, 6, 4, 5, 5, 9, 1};
for (int n : numbers) {
System.out.print(n + " ");
}
countSort(numbers, 9);
System.out.println();
for (int n : numbers) {
System.out.print(n + " ");
}
}
}