简体   繁体   中英

Counting Sort in Java

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;
    }
}

I am getting an ArrayOutofBoundsException. Please go through the code and suggest me something.

This is the wrong approach. What you do is:

  1. look at the stack trace from the exception ( your IDE will show it to you )

  2. look at the line in your code where the stack trace says the exception was thrown

  3. read the error message to find out what the index value was

  4. figure out how that index value could have arisen by reading the previous code

  5. if you can't figure it out from reading the code, use the IDE's debugger to single step the program and observe what the values of the variables / objects are and how they change.


If you posted an exception stacktrace, we could probably figure it out for you. In fact, if someone was prepared to spend some time they could possibly figure it out without a stack trace.

But that defeats the purpose of this as a learning exercise for you . You need to learn how to do this kind of stuff yourself ... by doing it yourself.


The original version of your question said this:

As per my IDE, I am getting an ArrayOutofBoundsException

I assumed since your IDE is telling you you are getting an exception, it would also show the exception message, and (if you click on it or something) the exception stacktrace. Certainly, my IDE is capable of doing that.

If that is not the case, the simple alternative is to put a try / catch block around the entire body of main like this.

public static void main(String[] args) throws Exception {
    try {
        // existing main body
        ...
    } catch (Exception ex) {
        ex.printStackTrace();
        throw ex;  // or leave this out.
    }
}

Seems like your issue resides here :

for ( int h = 0; h > b.length; h++) {
        System.out.println(b[h]);
    }

Your for loop is not working, since the condition is always false ie h > b.length, change that to :

for ( int h = 0; h < b.length; h++) {
        System.out.println(b[h]);
    }

Hope that might help.

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM