Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.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
气泡排序不起作用 package13; 导入java.util.Scanner; 公共类分拣机{ 公共静态void main(字符串[]args){ int i,j; 字符串选择; int-inputArray[]=新的int[10]; System.out.println(“输入10个数字:”); 扫描仪扫描=新扫描仪(System.in); 对于(i=0;i_Java_Bubble Sort - Fatal编程技术网

气泡排序不起作用 package13; 导入java.util.Scanner; 公共类分拣机{ 公共静态void main(字符串[]args){ int i,j; 字符串选择; int-inputArray[]=新的int[10]; System.out.println(“输入10个数字:”); 扫描仪扫描=新扫描仪(System.in); 对于(i=0;i

气泡排序不起作用 package13; 导入java.util.Scanner; 公共类分拣机{ 公共静态void main(字符串[]args){ int i,j; 字符串选择; int-inputArray[]=新的int[10]; System.out.println(“输入10个数字:”); 扫描仪扫描=新扫描仪(System.in); 对于(i=0;i,java,bubble-sort,Java,Bubble Sort,气泡排序有2个循环。您只有一个循环 您需要将一些循环嵌套在一起。我认为您应该编写自己的代码。但谁真正关心冒泡排序呢 气泡排序的解释 基本上,冒泡排序是通过检查相邻的值(彼此相邻的值)并根据第一个值是否更大进行交换来工作的。它不断地在列表(数组)中迭代,直到一切正常为止 简单嵌套循环 package package13; import java.util.Scanner; public class Sorter { public static void main (String[

气泡排序有2个循环。您只有一个循环


您需要将一些循环嵌套在一起。我认为您应该编写自己的代码。但谁真正关心冒泡排序呢


气泡排序的解释

基本上,冒泡排序是通过检查相邻的值(彼此相邻的值)并根据第一个值是否更大进行交换来工作的。它不断地在列表(数组)中迭代,直到一切正常为止


简单嵌套循环

package package13;

import java.util.Scanner;

public class Sorter {

    public static void main (String[] args) {


        int i,j;
        String select;

        int inputArray[]=new int[10];
        System.out.println("Enter 10 numbers:");
        Scanner scan = new Scanner (System.in);
        for (i=0;i<inputArray.length;i++) {
            inputArray[i]=scan.nextInt();
        }
        for (j=0;j<inputArray.length;j++) {
            System.out.print(" "+inputArray[j]);
        }
        System.out.println("\nHow would you like to sort these numbers?");
        System.out.println("Your choices are: Selection, Insertion, or Bubble");
        Scanner scanner = new Scanner (System.in);

        select = scanner.nextLine();
        String choice = select.toString();

        String answer=choice;

        //going to use a switch for this

        choices Choice = choices.valueOf(answer.toUpperCase());

        switch(Choice) {
        case SELECTION:
            System.out.println("Selection sort:");
            int min;

            for (int k=0; k < inputArray.length; k++) {
                min=k;
                for (int l=k +1;l<inputArray.length;l++) {
                    if (inputArray[l] < inputArray[min]) {
                        min=l;
                    }
                }
                if (min != k) {
                    final int temp = inputArray[k];
                    inputArray[k] = inputArray[min];
                    inputArray[min] = temp;
                }
                System.out.println(inputArray[k]);

            }
            break;
        case INSERTION:
            System.out.println("Insertion sort");
            int blue, temp;
            for (int red=1;red<inputArray.length;red++) {
                blue=red;
                temp=inputArray[red];
                while (blue!=0&&inputArray[blue-1]>temp) {
                    inputArray[blue]=inputArray[blue-1];
                    blue--;
                }
                inputArray[blue]=temp;
            }
            for (int green=0;green<inputArray.length;green++) {
                System.out.println(inputArray[green]);
            }
            break;
        case BUBBLE:
            System.out.println("Bubble sort");
            int cat, dog;
            boolean fixed=false;

            while (fixed==false) {
                fixed=true;

                for (dog=0;dog<inputArray.length;dog++) {
                    if (inputArray[dog] > inputArray[dog+1]) {
                        cat = inputArray[dog+1];
                        inputArray[dog+1]=inputArray[dog];
                        inputArray[dog]=cat;
                        fixed=false;
                    }
                }

            }

            for (int mouse=0;mouse<inputArray.length-1;mouse++) {
                System.out.println(inputArray[mouse]);
            }
            break;
        }

    }
    public enum choices {
        SELECTION,
        INSERTION,
        BUBBLE
    }

}
for(int i=0;iarrayInput[j+1]){
//交换值
}
}
}
边做边做

for(int i = 0; i < arrayInput.length; i++){
    for(int j = 0; j < arrayInput.length-1; j++){
        if(arrayInput[j] > arrayInput[j+1]){
            //swap values
        }
    }
}
boolean swapped=false;
做{
交换=错误
对于(int j=0;jarrayInput[j+1]){
//交换值
交换=真;
}
}
}while(交换);
递归的(刚刚编好这个:p)

Boolean swapped=true;
while(交换){
swapped=bubblehalper(arrayInput);
}
布尔bubblehelper(int[]arrayInput){
布尔交换=假;
对于(int j=0;jarrayInput[j+1]){
//交换值
交换=真;
}
}
返回交换;
}
希望这有帮助。

来自维基百科

Boolean swapped = true;
while(swapped){
    swapped = bubblehelper(arrayInput);
}

Boolean bubblehelper(int[] arrayInput){
      Boolean swapped = false;
      for(int j = 0; j < arrayInput.length-1; j++){
           if(arrayInput[j] > arrayInput[j+1]){
              //swap values
              swapped = true;
           }
       }
       return swapped;
}
boolean changed=false;
做{
更改=错误;
对于(int a=0;a0{
E tmp=可比[a];
可比[a]=可比[a+1];
可比[a+1]=tmp;
更改=正确;
}
}
}同时(改变);
你的密码

boolean changed = false;
    do {
        changed = false;
        for (int a = 0; a < comparable.length - 1; a++) {
            if (comparable[a].compareTo(comparable[a + 1]) > 0) {
                E tmp = comparable[a];
                comparable[a] = comparable[a + 1];
                comparable[a + 1] = tmp;
                changed = true;
            }
        }
    } while (changed);
boolean fixed=false;
while(fixed==false){
固定=正确;
for(dog=0;dog输入阵列[dog+1]){
cat=输入阵列[dog+1];
输入阵列[dog+1]=输入阵列[dog];
输入阵列[狗]=猫;
固定=假;
}
}
}

你能看出其中的区别吗?

它到底是如何不起作用的?
boolean changed = false;
    do {
        changed = false;
        for (int a = 0; a < comparable.length - 1; a++) {
            if (comparable[a].compareTo(comparable[a + 1]) > 0) {
                E tmp = comparable[a];
                comparable[a] = comparable[a + 1];
                comparable[a + 1] = tmp;
                changed = true;
            }
        }
    } while (changed);
        boolean fixed=false;

        while (fixed==false) {
            fixed=true;

            for (dog=0;dog<inputArray.length;dog++) {
                if (inputArray[dog] > inputArray[dog+1]) {
                    cat = inputArray[dog+1];
                    inputArray[dog+1]=inputArray[dog];
                    inputArray[dog]=cat;
                    fixed=false;
                }
            }

        }