如何使冒泡排序在Java中输出的分类数字?(How to make Bubble Sort in J

2019-09-01 08:05发布

这是我对冒泡排序的代码。 我不能得到实际排序值输出。 该程序读取输入的号码,但并不打印排序。 我不知道我有什么做的,让他们排序。 任何意见或建议将是有益的。

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

}

}

Answer 1:

试试这个冒泡排序:

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;

        }
    }
  }
}


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


Answer 3:

试试这个 :

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


Answer 4:

你传递数组变量num(这是不是静态)到冒泡()(不返回一个值和阴影与自己全球num变量),并试图使用相同的num变量从您的主要访问数组排序方法,该方法是不正确的。 如果是真正的修复程序,这是声明你的变量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;
            }

}

}



文章来源: How to make Bubble Sort in Java to output the sorted numbers?