-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathCountingSort.java
More file actions
42 lines (34 loc) · 1.15 KB
/
CountingSort.java
File metadata and controls
42 lines (34 loc) · 1.15 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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
class CountSort{
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the size of Array to be sorted");
int n = Integer.parseInt(br.readLine());
System.out.println("Enter the Array to be sorted");
int[] arr = new int[n];
String[] integerStrings = br.readLine().split(" ");
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(integerStrings[i]);
}
int k = 60;
countingSort(arr, k);
System.out.println("Sorted Array :- ");
System.out.println(Arrays.toString(arr));
}
static void countingSort(int[] input, int k) {
// create buckets
int count[] = new int[k + 1];
for (int i : input) {
count[i]++;
}
int ndx = 0;
for (int i = 0; i < count.length; i++) {
while (0 < count[i]) {
input[ndx++] = i;
count[i]--;
}
}
}
}