Principle: To compare two adjacent elements, large elements of the right to exchange value.
Thinking: sequentially comparing the number of two adjacent, the numbers are placed ahead of large numbers on the back. I.e. the first pass: First, a first comparator and a second number, the decimal place before, after the release of large numbers. Then compare the second number and a third number, the decimal place before and after the release of large numbers, so continues until the last two numbers compare, before the decimal place, after the release of large numbers. Trip Repeat steps until all the sort is complete.
After the first pass completion of the comparison, the last digit must be the biggest of a number of the array, so the second trip when comparing the last number is not involved in the comparison;
After the second pass completion of the comparison, the penultimate number, must be the second largest number in the array, so the third trip when comparing the last two numbers do not participate in the comparison;
And so on, each pass number of comparisons 1;
……
For example: To sort the array: int [] arr = {6,3,8,2,9,1 };
first pass sort:
The first sort: 6 and Comparative 3, 6 is greater than 3, switching positions: 368,291
The second sort: 6 and 8 compared to less than 6 8, no exchange location: 368291
Third Sort: 8 and Comparative 2, 8 is greater than 2, the exchange location: 362891
Fourth Sort: 8 and 9 compare 8 less than 9, no switching positions: 362,891
Fifth Sort: 9 and Comparison 1: 9 greater than 1, switching positions: 362,819
First pass total of five times compared sorting results: 362,819
The second trip Sort:
The first sort: 3 and 6 compared to less than 3 6, do not exchange positions: 362,819
The second sort: 6 and Comparison 2, more than 2 6, exchange positions: 326,819
Third Sort: comparison 6 and 8, 6 is greater than 8, no exchange location: 326819
Fourth Sort: 8 and Comparative 1, 8 is greater than 1, switching positions: 326,189
A total of 4 second pass comparisons, sorting results: 326,189
The third trip Sort:
The first sort: 3 and comparison 2, 3 is greater than 2, the exchange location: 236189
The second sort: 3 and 6 compared to less than 3 6, do not exchange positions: 236,189
Third Sort: 6 and Comparative 1, 6 is greater than 1, switching positions: 231,689
The second pass for a total of three times compared to the results of the sort: 231689
Fourth trip Sort:
The first sort: 2 and 3 compared to less than 2 3 not change position: 231689
The second sort: 3 and Comparison 1, 3 is greater than 1, switching positions: 213,689
A total of 2 second pass comparisons, sorting results: 213,689
The fifth trip Sort:
The first sort: 1 and Comparative 2, is greater than 1, switching positions: 123,689
A total of 1 second pass comparisons, sorting results: 123,689
The end result: 123689
Thus: N numbers to sort is complete, for a total of N-1 times sort, every sort of pass i is the number (Ni) times, it is possible to use a double loop, how many times the outer control loop, the control of each inner layer times the number of cycles, that is,
/*
* 冒泡排序
*/
public class BubbleSort {
public static void main(String[] args) {
int[] arr={6,3,8,2,9,1};
System.out.println("排序前数组为:");
for(int num:arr){
System.out.print(num+" ");
}
for(int i=0;i<arr.length-1;i++){//外层循环控制排序趟数
for(int j=0;j<arr.length-1-i;j++){//内层循环控制每一趟排序多少次
if(arr[j]>arr[j+1]){
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
System.out.println();
System.out.println("排序后的数组为:");
for(int num:arr){
System.out.print(num+" ");
}
}
}