对堆栈溢出进行排序以及比较和交换次数为负数

发布于 2024-12-04 21:51:27 字数 1324 浏览 6 评论 0原文

我编写了这个冒泡排序并将其用在一个测试程序中,该程序按照用户输入的数量在列表中给出随机数。然后给它一个包含 10,000 个随机整数的列表,并在第 55 行返回堆栈溢出“if (swaps != 0){sort();}”这是为什么。有时它也有效,但返回 myCompares 和 mySwaps 的值为负值。你能帮忙吗?

public class Bubbly {

    private int[] sortedList;
    private static long myTime = 0;
    private static int myCompares = 0;
    private static int mySwaps = 0;

    public Bubbly(int[] list) {
        sortedList = list;
        StopWatch stop = new StopWatch();
        stop.start();
        sort();
        stop.stop();
        myTime = stop.getElapsedTime();
    }

    public int[] getList(){
        return sortedList;
    }
    public long getTime(){
        return myTime;
    }

    public int getCompares(){
        return myCompares;
    }

    public int getSwaps(){
        return mySwaps;
    }

    public void sort(){
        int length = sortedList.length, i = 0, num, swaps = 0;

        while (i < length - 1){
            if (sortedList[i] > sortedList[i + 1]) {
                myCompares++;

                num = sortedList[i];
                sortedList[i] = sortedList[i+1];
                sortedList[i+1] = num;
                swaps++;
                mySwaps++;
            }
            myCompares++;
            i++;
        }

        if (swaps != 0){
            sort();
        }

    }
}

I wrote this bubble sort and used it in a test program that gives random numbers in a list by an amount inputted by the user. It was then given a list of 10,000 random ints and came back with a stack overflow at line 55 "if (swaps != 0){sort();}" why is this. Also at times it works but gives back a value for myCompares and mySwaps that is negative. Can you help?

public class Bubbly {

    private int[] sortedList;
    private static long myTime = 0;
    private static int myCompares = 0;
    private static int mySwaps = 0;

    public Bubbly(int[] list) {
        sortedList = list;
        StopWatch stop = new StopWatch();
        stop.start();
        sort();
        stop.stop();
        myTime = stop.getElapsedTime();
    }

    public int[] getList(){
        return sortedList;
    }
    public long getTime(){
        return myTime;
    }

    public int getCompares(){
        return myCompares;
    }

    public int getSwaps(){
        return mySwaps;
    }

    public void sort(){
        int length = sortedList.length, i = 0, num, swaps = 0;

        while (i < length - 1){
            if (sortedList[i] > sortedList[i + 1]) {
                myCompares++;

                num = sortedList[i];
                sortedList[i] = sortedList[i+1];
                sortedList[i+1] = num;
                swaps++;
                mySwaps++;
            }
            myCompares++;
            i++;
        }

        if (swaps != 0){
            sort();
        }

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝礼 2024-12-11 21:51:27

您的程序是递归的,可能是我见过的第一个递归冒泡排序:-)

递归意味着该函数在工作完成之前不会返回,而是每次调用 sort() 时都会将额外的调用推送到堆栈上。经过多次递归调用后,堆栈已满并溢出。

所以,去掉递归,这里没用,用循环就可以了。

对于获得负值的变量,首先要去掉 mySwaps、myTime 和 myCompare 上的 static 修饰符,因为它会阻止它们在每次测试运行时正确初始化。

Your program is recursive, probably the first recursive bubblesort I've ever seen :-)

Recursive implies that the function doesn't return until the work is done, instead each time sort() is called an extra call is pushed onto the stack. And after a number of recursive calls the stack is full and overflows.

So, get rid of the recursion, it's not useful here, just use a loop.

Regarding the variables that get negative values, start by getting rid of the static modifier on mySwaps, myTime and myCompare as it inhibits their correct initialization on each test run.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文