简体   繁体   English

用Java计算排序

[英]Counting Sort in Java

Please help me with the following implementation of Counting Sort in Java. 请帮助我实现Java中的计数排序的以下实现。 I am new to Java and debugging, so I am not sure about the error. 我是Java和调试的新手,所以我不确定该错误。 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. 我收到了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 ) 查看异常中的堆栈跟踪(您的IDE会向您显示)

  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. 如果您无法通过阅读代码来解决问题,请使用IDE的调试器来单步执行程序,并观察变量/对象的值是什么以及它们如何更改。


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 根据我的IDE,我得到一个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. 我假设由于您的IDE告诉您您正在获取异常,所以它还会显示异常消息,以及(如果您单击它或其他内容)异常堆栈跟踪。 Certainly, my IDE is capable of doing that. 当然,我的IDE可以做到这一点。

If that is not the case, the simple alternative is to put a try / catch block around the entire body of main like this. 如果不是的话,简单的办法是把一个try / catch块周围的整个身体main是这样。

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循环无法正常工作,因为条件始终为false,即h> b.length,请将其更改为:

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

Hope that might help. 希望对您有所帮助。

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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