-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount Sort
64 lines (55 loc) · 1.34 KB
/
Count Sort
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
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.io.*;
import java.util.*;
public class Main {
public static void countSort(int[] arr, int min, int max) {
//write your code here
int range = max-min+1;
int[] free = new int[range];
for(int i = 0;i<arr.length;i++){
int idx = arr[i];
free[idx]++;
}
for(int i = 1;i<arr.length;i++){
free[i] = free[i]+free[i-1];
}
int [] ans = new int [arr.length];
for(int i = arr.length-1;i>=0;i--){
int val = arr[i];
int pos = free[val-min];
int idx = pos-1;
ans[idx] = val;
}
}
public static void print(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
int z =0;
int j = 0;
for(int i = 0;i<arr.length;i++){
for(int k =0;k<arr.length;k++){
if(arr[i]==arr[j]){
return;
i++;
}else{
arr[i]!=arr[j];
j++;
}
}
}
}
}
public static void main(String[] args) throws Exception {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int[] arr = new int[n];
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
arr[i] = scn.nextInt();
max = Math.max(max, arr[i]);
min = Math.min(min, arr[i]);
}
countSort(arr,min,max);
print(arr);
}
}