0

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

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

4 回答 4

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;

        }
    }
  }
}
于 2013-04-28T19:12:32.230 回答
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;
}
于 2013-04-28T19:12:41.487 回答
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]);
            }
        }
    }
于 2014-01-30T13:10:06.497 回答
0

您正在将数组变量 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;
            }

}

}

于 2013-04-28T19:52:41.723 回答