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
}
}
Предполагается, что он попросит пользователя ввести десять чисел с использованием массивов. Затем программа запрашивает у пользователя, какой алгоритм сортировки будет использоваться для сортировки этих чисел. Наконец, программа печатает массив, отсортированный выбранным пользователем алгоритма. Я не могу понять, почему сортировка пузыря не работает любая помощь?Bubble sort не работает
Как именно он не работает? –