In: Computer Science
Use counting sort, sort the following numbers: 4, 2, 5, 4, 2, 3, 0, 2, 4, 3
Code:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int a[]=new int[10];
int c[]=new int[10];
int i,j,k;
Scanner sc=new Scanner(System.in);
System.out.println("Enter numbers::");
for(i=0;i<10;i++)
{
a[i]=sc.nextInt();
/*Assign each element Array C with 0*/
c[i]=0;
}
for(i=0;i<10;i++)
{
/* Read each elemenet of array A and assign it to k*/
k=a[i];
/*Increment c[k] by 1*/
c[k]=c[k]+1;
}
System.out.println("--After Sorting--");
for(i=0;i<10;i++)
{
for(j=0;j<c[i];j++)
{
System.out.print(i+" ");
}
}
}
}
Screenshots: