Package datastructure.maskedmen.sort;
Import Java.util.Arrays;
Import Java.util.Scanner;
/**
* @author Maskedmen
* Bubble Sort Time complexity: O (n2)
*/
public class Bubblesort {
public static void Main (string[] args) {
Bubblesort ();//Bubble sort
}
Bubble sort this sort of need sort n-1 round
public static void Bubblesort () {
System.out.println ("Please enter the number of integers that need to be sorted:");
Scanner input = new Scanner (system.in);
int length = Input.nextint ();
int[] Number = new Int[length];
int count=1;
int temp = 0;//element Exchange Intermediate variable
while (true) {
if (count>length) {
Break
}
System.out.println ("+count+", "A:");
Number[count-1] = Input.nextint ();
count++;
}
System.out.println (the "+length+" integer you entered is: "+arrays.tostring (number)");
System.out.println ("\n*************************************\n");
for (int j=0;j<number.length-1;j++) {//length-1 Wheel comparison
for (int i=0;i<number.length-1-j;i++) {//= a round comparison, after each wheel comparison, the maximum number of each round has been found, so the number of comparisons will be reduced by 1
if (Number[i]>number[i+1]) {
temp = Number[i];
NUMBER[I]=NUMBER[I+1];
Number[i+1]=temp;
}
}
}
System.out.println ("sorted" +length+ "integers are:" +arrays.tostring (number));
}
}
This article is from the "maskedmen" blog, make sure to keep this source http://maskedmen.blog.51cto.com/8100866/1745345
Java Bubble sort