-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathCountSort.java
More file actions
33 lines (31 loc) · 970 Bytes
/
CountSort.java
File metadata and controls
33 lines (31 loc) · 970 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
public class CountSort {
public static void countingsort(int arr[]) {
int max = Integer.MIN_VALUE;
// Finding the Maximum value of orignal Array
for(int i=0;i<arr.length;i++){
max = Math.max(arr[i], max);
}
//Creating a Frequency array of Length of Maximum value +1 of Orignal Array
int count [] = new int[max+1];
for(int i=0;i<arr.length;i++){
count[arr[i]]++;
}
// Sorting
int j=0;
for(int i=0;i<count.length;i++){
while(count[i]>0){
arr[j]=i;
j++;
count[i]--;
}
}
// Returning the Sorted Array
for(int i=0;i<arr.length ;i++){
System.out.print(arr[i]+", ");
}
}
public static void main(String[] args) {
int arr[] = {1,4,1,4,2,3,7}; // Test Array
countingsort(arr); // Calling Function
}
}