如何在 Java 中进行冒泡排序以输出排序后的数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16266611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:04:37  来源:igfitidea点击:

How to make Bubble Sort in Java to output the sorted numbers?

javabubble-sort

提问by user2329764

This is my code for the Bubble Sort. I cannot get the actual sorted values to output. The program reads the inputted numbers, but does not print it sorted. I'm not sure what I have to do to make them sort. Any advice or suggestions would be helpful.

这是我的冒泡排序代码。我无法输出实际的排序值。程序读取输入的数字,但不会按顺序打印。我不确定我必须做什么才能让它们排序。任何意见或建议都会有所帮助。

package sortingalgorithm2;
import java.util.Scanner;

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args)
{
    Scanner read = new Scanner (System.in);
    int[] num = new int[15];
    int size = 15;

    System.out.println("Enter 15 numbers: ");
    for (int i=0; i <= size-1; i++)
    {
        num[i] = read.nextInt();

    }

    for (int i=0; i <= size-1; i++)
    {
        if (num[i] >=1 && num[i] <= 1000)
       {
        System.out.println("The numbers you entered are: ");
        System.out.println(+num[0]);
        System.out.println(+num[1]);
        System.out.println(+num[2]);
        System.out.println(+num[3]);
        System.out.println(+num[4]);
        System.out.println(+num[5]);
        System.out.println(+num[6]);
        System.out.println(+num[7]);
        System.out.println(+num[8]);
        System.out.println(+num[9]);
        System.out.println(+num[10]);
        System.out.println(+num[11]);
        System.out.println(+num[12]);
        System.out.println(+num[13]);
        System.out.println(+num[14]);
     }
    else
    {
        System.out.println("Data input is invalid. Enter a number between "
                +
                "1 and 1000.");
        break;
    }
    }

    BubbleSort (num);
    for (int i=0; i < num.length; i++)
    {
        System.out.println("The sorted numbers are: ");
        System.out.print(num[i]+ " ");
    }

}

private static void BubbleSort(int[] num)
{
    for (int i=0; i <= num.length; i++)
        for (int x=1; x <= num.length; x++)
            if (num[x] > num[x+1])
            {
                int temp = num[x];
                num[x] = num[x+1];
                num[x+1] = temp;
            }

}

}

采纳答案by Alya'a Gamal

Try this Bubble sort :

试试这个冒泡排序:

private static void BubbleSort(int[] num) {
 for (int i = 0; i < num.length; i++) {
    for (int x = 1; x < num.length - i; x++) {
        if (num[x - 1] > num[x]) {
            int temp = num[x - 1];
            num[x - 1] = num[x];
            num[x] = temp;

        }
    }
  }
}

回答by Semih Yagcioglu

You are printing the actual numbers in the order the user entered. Try this instead:

您正在按照用户输入的顺序打印实际数字。试试这个:

int[] sortedNumbers = new int[15];

sortedNumbers = BubbleSort (num);

    for (int i=0; i < sortedNumbers.length; i++)
    {
        System.out.println("The sorted numbers are: ");
        System.out.print(sortedNumbers[i]+ " ");
    }



 public static int[] BubbleSort(int [] num)
{
    int temp;   
    for (int i=1; i<num.length; i++)
    {
        for(int j=0; j<num.length-i; j++)
        {
            if (num[j] > num [j+1])
            {
                temp = num [j];
                num [j] = num [j+1];
                num [j+1] = temp;
            }
        }
    }

    return num;
}

回答by Sushim Mukul Dutta

you are passing the array variable num (which is not static) to BubbleSort()(which does not returns a value and shadows the global num variable with its own) and trying to use the same num variable to access your sorted array from your main method which is not right. The genuine fix to this is to declare your variable num as static just before the main method( in the class declaration). So I have made the changes in the program and here is the solution.

您正在将数组变量 num(不是静态的)传递给 BubbleSort()(它不返回值并用它自己的阴影全局 num 变量)并尝试使用相同的 num 变量从您的主访问您的排序数组方法不对。真正的解决方法是在 main 方法之前(在类声明中)将变量 num 声明为静态变量。所以我在程序中进行了更改,这是解决方案。

import java.util.Scanner;

public class sol {

static int num [] =new int [15]; //declaring num as static in the class definition.
public static void main(String[] args)
{
    Scanner read = new Scanner (System.in);
    int size = 15;

    System.out.println("Enter 15 numbers: ");
    for (int i=0; i <= size-1; i++)
    {
        num[i] = read.nextInt();

    }
    read.close();
    /*for (int i=0; i <= size-1; i++)
{


    if (num[i] >=1 && num[i] <= 1000)
   {
    System.out.println("The numbers you entered are: ");
    System.out.println(+num[0]);
    System.out.println(+num[1]);
    System.out.println(+num[2]);
    System.out.println(+num[3]);
    System.out.println(+num[4]);
    System.out.println(+num[5]);
    System.out.println(+num[6]);
    System.out.println(+num[7]);
    System.out.println(+num[8]);
    System.out.println(+num[9]);
    System.out.println(+num[10]);
    System.out.println(+num[11]);
    System.out.println(+num[12]);
    System.out.println(+num[13]);
    System.out.println(+num[14]);
 }
else
{
    System.out.println("Data input is invalid. Enter a number between "
            +
            "1 and 1000.");
    break;
}
}*/ //I have disabled this just to check with the sort method.

    BubbleSort ();//no need to pass the array as it is static and declared as a      //class variable hence can be used to by all the methods of that class
    System.out.println("The sorted numbers are: ");
    for (int i=0; i < num.length; i++)
    {

        System.out.print(num[i]+ " ");
    }

}

private static void BubbleSort()
{
    for (int i=0; i < num.length; i++)// required changes in the looping
        for (int x=0; x < num.length-i-1; x++)
            if (num[x] > num[x+1])
            {
                int temp = num[x];
                num[x] = num[x+1];
                num[x+1] = temp;
            }

}

}

}

回答by Syed Haziq Hamdani

Try this :

尝试这个 :

    for (int i = 0; i < num.length; i++) {
        for (int j = i + 1; j < num.length; j++) {
            if (num[i] > num[j]) {
                num[i] = num[i] + num[j] - (num[j] = num[i]);
            }
        }
    }