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

时间:2013-04-28 19:04:55

标签: java bubble-sort

这是我的冒泡排序代码。我无法获得输出的实际排序值。程序读取输入的数字,但不打印排序。 我不确定我要做些什么来让它们排序。 任何建议或建议都会有所帮助。

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

}

}

4 个答案:

答案 0 :(得分:3)

试试这个冒泡排序:

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;

        }
    }
  }
}

答案 1 :(得分:2)

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

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

答案 2 :(得分:2)

试试这个:

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

答案 3 :(得分:0)

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

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

}

}