private static int range;
private static int[] CountSortf(int[]array) {
int k=0;
int[]values=new int[range+1];
for(int i=0;i<=range;i++)
{
values[i]=0;
}
for(int i=0;i<array.length;i++)
{
values[array[i]]++;
}
int[]arr=new int[array.length];
for(int i=0;i<=range;i++)
{
for(int j=0;j<values[i];j++)
{
arr[k++]=i;
}
}
return arr;
}
public static void main(String[] args) {
range=9;
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
int []array=new int[n];
for(int i=0;i<n;i++)
{
array[i]=scn.nextInt();
}
array=CountSortf(array);
for(int i=0;i<array.length;i++)
{
System.out.print(array[i]+" ");
}
}
Why this code seems to be wrong
refer to y last submission also
@Syed-Siddiqui-2497215583854102,
you need to find the max element in the array first and values array will be of size (max_element+1).
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.