-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountSort.java
More file actions
40 lines (33 loc) · 1023 Bytes
/
Copy pathcountSort.java
File metadata and controls
40 lines (33 loc) · 1023 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
36
37
38
39
40
import java.util.*;
class countSort {
public static void main(String[] args) {
int[] A = { 1, 5, 4, 9, 3, 3, 8, 5, 1, 6, 4, 5, 44, 5, 87, 4, 5, 55, 1 };
sort(A);
}
// count sort
// first store frequency of each element
// / then loop through the count and print each frequency
static void sort(int[] arr) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
if (map.containsKey(arr[i])) {
int freq = map.get(arr[i]);
map.put(arr[i], ++freq);
} else {
map.put(arr[i], 1);
}
}
// once hashmap is done now print based on hashmap
// System.out.println(map);
for (int i = 1; i <= 87; i++) {
if (map.containsKey(i)) {
int frequency = map.get(i);
for (int j = 1; j <= frequency; j++) {
System.out.print(i);
}
}
}
// System.out.println(arr);
}
}
// Time Complexity : O(N + )