Please help me with the following implementation of Counting Sort in Java. I am new to Java and debugging, so I am not sure about the error. The problem with the code below is that although it compiles, I do not get any output on screen. Please go through the code and suggest me something. Perhaps there is some logical error. Thanks
import java.io.*;
import java.lang.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Cnt {
public static void main(String[] args) throws java.lang.Exception {
BufferedReader R = new BufferedReader(new InputStreamReader(System.in));
int[] a ;
int[] b;
String inp = R.readLine();
int N = Integer.parseInt(inp);
a = new int[N];
b = new int[N];
for ( int i = 0; i< N; i++) {
a[i] = Integer.parseInt(R.readLine());
}
int key = findmax(a);
int k = key+1;
int c[] = new int[k];
for ( int i = 0; i < k; i++) {
c[i] = 0;
}
for ( int j = 0; j < a.length; j++){
c[a[j]] = c[a[j]] +1;
}
for ( int i = 1; i < key ; i++) {
c[i] = c[i] + c[i-1];
}
for ( int j = (a.length - 1); j >=0; j--) {
b[c[a[j]]] = a[j];
c[a[j]]= c[a[j]] -1;
}
//System.out.println(b[0]);
for ( int h = 0; h > b.length; h++) {
System.out.println(b[h]);
}
}
private static int findmax(int a[])
{
int r;
r = a[0];
for ( int i =0; i < a.length; i++ ) {
if (a[i] >= r) {
r = a[i];
}
}
return r;
}
}
Seems like your issue resides here :
Your for loop is not working, since the condition is always false i.e. h > b.length, change that to :
Hope that might help.
Regards