Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java编写冒泡排序_Java_Sorting_Bubble Sort - Fatal编程技术网

使用Java编写冒泡排序

使用Java编写冒泡排序,java,sorting,bubble-sort,Java,Sorting,Bubble Sort,我不知道我的编码是否正确,但有人能确认我的dobblesort方法及其在main方法中的实现是否正确编程吗?我的编码要求我创建一个大小为20的数组,并用1到1000之间的随机整数填充它,而无需硬编码。结果应显示原始的未排序整数列表;然后在单独的行上显示气泡排序算法的每个过程。我必须重复该程序,直到用户选择退出**我已经进行了编辑,以确保无论我使用什么变量,它都是根据ArrayList声明的 下面是我希望输出结果的一个示例(尽管当我尝试执行20个整数时,它只显示5个整数): 未分类列表:68329

我不知道我的编码是否正确,但有人能确认我的dobblesort方法及其在main方法中的实现是否正确编程吗?我的编码要求我创建一个大小为20的数组,并用1到1000之间的随机整数填充它,而无需硬编码。结果应显示原始的未排序整数列表;然后在单独的行上显示气泡排序算法的每个过程。我必须重复该程序,直到用户选择退出**我已经进行了编辑,以确保无论我使用什么变量,它都是根据ArrayList声明的

下面是我希望输出结果的一个示例(尽管当我尝试执行20个整数时,它只显示5个整数):

未分类列表:6832982901
通过1:3682901298
通过2:3681290298
通过3:3168290298
通过4:1368290298

//用于捕获键盘输入
导入java.util.*;
//我们班叫泡泡运动
公共类泡泡糖{
//创建多气泡排序方法
公共静态void dobblesort(ArrayList arr){
布尔值needNextPass=true;
while(needNextPass){
//数组可能已排序,不需要下一次传递
needNextPass=false;
//交换清单
对于(int i=0;iarr.get(i+1)){
int temp=arr.get(i);
arr.set(i,arr.get(i+1));
阵列设置(i+1,温度);
打印输出(i+1,arr);//使用打印输出方法
needNextPass=true;//仍需要下一次传递
}
}
}
}
私有静态void打印输出(int pass,ArrayList列表){
系统输出打印(“通过”+通过+:”;
对于(int i=0;i
您为气泡排序编写了太多的锅炉板代码。对于冒泡排序,使用递归方法。我为你们写了一个简单的泡泡方法,用输出做你们想做的

private int[] bubbleSort(int[] arr){

    int c;
    boolean isArranged = false;

    for (int i = 0; i < arr.length; i++) {
        if (i < (arr.length - 1) && arr[i] > arr[i+1]){
            c = arr[i];
            arr[i] = arr[i+1];
            arr[i+1] = c;
            isArranged = true;
        }
    }

    if (isArranged){
        return bubbleSort(arr);
    }else{
        return arr;
    }
}
private int[]bubbleSort(int[]arr){
INTC;
布尔值isArranged=false;
对于(int i=0;iarr[i+1]){
c=arr[i];
arr[i]=arr[i+1];
arr[i+1]=c;
isArranged=true;
}
}
如果(已安排){
返回气泡分离器(arr);
}否则{
返回arr;
}
}
将其称为:

Scanner in = new Scanner(System.in);
    int length = in.nextInt();
    int[] arr = new int[length];

    for (int i = 0; i < length; i++) {
        arr[i] = in.nextInt();
    }

    Main main = new Main();

    int[] newArr = main.bubbleSort(arr);

    for (int i = 0; i < newArr.length; i++) {
        System.out.print(newArr[i] + " ");
    }
Scanner-in=新的扫描仪(System.in);
int length=in.nextInt();
int[]arr=新的int[长度];
for(int i=0;i

您可以编写ArrayList而不是int array

按照您的实现进行,因为您似乎刚刚了解了这一点,所以有几件事您应该改变。首先,由于dobblesort方法使用int数组,因此在main方法中也使用int数组

bubblesort的实现也需要改变。你应该首先仔细研究它的逻辑。不必每次都检查整个阵列

// Create doBubbleSort method 
public static void doBubbleSort(int[] arr) {
    boolean needNextPass = true;
    // Array may be sorted and next pass not needed 
    // Swap list
    for (int i = 0; i < arr.length - 1; i++) {
        if (needNextPass) {
            needNextPass = false;
            for (int j = arr.length - 1; j > i; j--) {
                int temp;
                if (arr[j] < arr[j - 1]) {
                    temp = arr[j - 1];
                    arr[j - 1] = arr[j];
                    arr[j] = temp;
                    needNextPass = true; // Next pass still needed
                }
            }
            printOut(i + 1, arr); // using printOut method
        }
    }
}
//创建dobbleSort方法
公共静态void dobblesort(int[]arr){
布尔值needNextPass=true;
//数组可能已排序,不需要下一次传递
//交换清单
对于(int i=0;ii;j--){
// Create doBubbleSort method 
public static void doBubbleSort(int[] arr) {
    boolean needNextPass = true;
    // Array may be sorted and next pass not needed 
    // Swap list
    for (int i = 0; i < arr.length - 1; i++) {
        if (needNextPass) {
            needNextPass = false;
            for (int j = arr.length - 1; j > i; j--) {
                int temp;
                if (arr[j] < arr[j - 1]) {
                    temp = arr[j - 1];
                    arr[j - 1] = arr[j];
                    arr[j] = temp;
                    needNextPass = true; // Next pass still needed
                }
            }
            printOut(i + 1, arr); // using printOut method
        }
    }
}
private static void printOut(int pass, int[] list) {
    System.out.print("PASS " + pass + ": ");
    for (int i = 0; i < list.length - 1; i++) {
        System.out.print(list[i] + ", ");
    }
    // Shows very last integer with a period
    System.out.print(list[list.length - 1] + ".");
    System.out.println();
}
// Main method
public static void main(String[] args) {
    int[] array = new int[20]; // Declare and instantiate a new ArrayList object
    Scanner userChoice = new Scanner(System.in); // User input for quitting program
    boolean inputFlag = true; // True if input is valid, false otherwise
    String choice;

    // Repeat program until user chooses to quit
    while (inputFlag == true) {

        try {
            /* Create an array of size 20 and populate it with random integers between 1 and 1000.
             Do not ask user for the numbers and do not hard code them */
            for (int i = 0; i < 20; i++) {
                int integer = (int) (1000.0 * Math.random());
                array[i] = integer;
            }
            System.out.print("\nUNSORTED LIST: ");

            //Display the 20 size of the unsorted ArrayList 
            for (int i = 0; i < array.length - 1; i++) {
                System.out.print(array[i] + ", ");
            }
            // Shows very last integer with a period
            System.out.print(array[array.length - 1] + ".");
            System.out.println();
            doBubbleSort(array);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("\nThere is an out of bounds error in the ArrayList.");
        }

        System.out.print("\nWould you like to continue the program? (Y/N): ");
        choice = userChoice.nextLine();

        while (!(choice.equalsIgnoreCase("Y")) && !(choice.equalsIgnoreCase("N"))) {
            // Error message when inputting anything other than Y/N
            System.out.println("\nERROR. Only Y, y, N, or n may be inputted.");
            System.out.println("Please try again.");
            choice = userChoice.nextLine();
        }

        if (choice.equalsIgnoreCase("N")) {
            inputFlag = false;
        }

    }
}