Java 中使用 BubbleSort 进行数组排序时出错

发布于 2024-12-20 15:21:08 字数 1693 浏览 4 评论 0原文

我正在尝试创建一个数组,其“总”数量在最小值和最大值之间。然后,使用冒泡排序对它们进行排序。当我执行时,我得到全零。有人能发现出了什么问题吗?若能及时回复,我们将不胜感激。

import java.util.*;
import java.util.Random;

public class final_project 
{ 
    public static void main(String[] args) 
    { 
        int numbers[]; 
        int i, min, max, total; 
        int num;
        Scanner scan = new Scanner(System.in);

        System.out.println("Please enter a minimum random value");
        min = scan.nextInt();
        System.out.println("Please enter a maximum random value");
        max = scan.nextInt();
        System.out.println("Please enter the amount of random numbers");
        total = scan.nextInt();

        numbers = new int[total];


        i = 0;
        total = 0;
        while ( i < total )
        {
            num = min + (int)(Math.random()*max);;
            numbers[i] = num;  
            total += num; 

            i += 1; /* i = i + 1; */
        }


        bubbleSort(numbers, numbers.length); 
        System.out.println("Your Sorted Array Is: "); 
        for(i=0; i<numbers.length; i++) 
        { 
            System.out.print(numbers[i] + " "); 
        } 

    }



    private static void bubbleSort(int[] numbers, int length) 
    { 
        int temp, counter, index; 

        for(counter=0; counter<length-1; counter++) 
        { 
            for(index=0; index<length-1-counter; index++) 
            { 
                if(numbers[index] > numbers[index+1]) 
                { 
                    temp = numbers[index]; 
                    numbers[index] = numbers[index+1]; 
                    numbers[index+1] = temp; 
                } 
            } 
        } 
    } 
}

I am trying to create an array with 'total' amount of numbers between min and max. And then, sort them using bubble sort. When i execute, i get all zeros. Could someone find what is going wrong? A prompt reply would be appreciated.

import java.util.*;
import java.util.Random;

public class final_project 
{ 
    public static void main(String[] args) 
    { 
        int numbers[]; 
        int i, min, max, total; 
        int num;
        Scanner scan = new Scanner(System.in);

        System.out.println("Please enter a minimum random value");
        min = scan.nextInt();
        System.out.println("Please enter a maximum random value");
        max = scan.nextInt();
        System.out.println("Please enter the amount of random numbers");
        total = scan.nextInt();

        numbers = new int[total];


        i = 0;
        total = 0;
        while ( i < total )
        {
            num = min + (int)(Math.random()*max);;
            numbers[i] = num;  
            total += num; 

            i += 1; /* i = i + 1; */
        }


        bubbleSort(numbers, numbers.length); 
        System.out.println("Your Sorted Array Is: "); 
        for(i=0; i<numbers.length; i++) 
        { 
            System.out.print(numbers[i] + " "); 
        } 

    }



    private static void bubbleSort(int[] numbers, int length) 
    { 
        int temp, counter, index; 

        for(counter=0; counter<length-1; counter++) 
        { 
            for(index=0; index<length-1-counter; index++) 
            { 
                if(numbers[index] > numbers[index+1]) 
                { 
                    temp = numbers[index]; 
                    numbers[index] = numbers[index+1]; 
                    numbers[index+1] = temp; 
                } 
            } 
        } 
    } 
}

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

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

发布评论

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

评论(3

落墨 2024-12-27 15:21:08

更改

while ( i < total )

while ( i < numbers.length )

Change

while ( i < total )

to

while ( i < numbers.length )
池予 2024-12-27 15:21:08

您的循环不执行:

        i = 0;
        total = 0;
        while ( i < total )
            ...

您也不想增加 total。将循环替换为:

        for(int i = 0; i < numbers.length; ++i)
        {
             numbers[i] = num;  
        }

Your loop doesn't execute:

        i = 0;
        total = 0;
        while ( i < total )
            ...

Also you don't want to increment total. Replace your loop with:

        for(int i = 0; i < numbers.length; ++i)
        {
             numbers[i] = num;  
        }
落日海湾 2024-12-27 15:21:08

您应该考虑使用 for 循环而不是 while 循环。 for 循环非常适合迭代数组。

You should consider using a for loop instead of a while loop. A for loop is perfect for iterating through an array.

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