Count Sort
class counting{
public static void main(String args[]){
int arr[]={1,4,1,3,2,4,3,7};
int n=arr.length;
int max=Integer.MIN_VALUE;
for(int i=0;i<arr.length;i++){
max=Math.max(max,arr[i]);
}
int count[]=new int[max+1];
for(int i=0;i<n;i++){
count[arr[i]]++;
}
System.out.println("Count array is ");
for(int i=0;i<n;i++)
System.out.print(count[i]);
// { 0 , 2 ,1 ,2, 2, 0 ,0, 1}
System.out.println(" ");
int z=0;
for(int i=0;i<n;i++){
while(count[i]>0){
arr[z]=i;
z++;
count[i]--;
}
}
System.out.println("Final array is")
for(int i=0;i<n;i++)
System.out.print(arr[i]);
}
}
Comments
Post a Comment