Principle : To compare the two adjacent values, will exchange a large element values to the right
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 completion of the first trip, the last a certain number of the largest array, so the trip does not participate in the second comparison,
After the second trip is completed, the penultimate number of the array must be the biggest, so the comparison does not participate in the third trip,
So, the number of comparisons per trip -1
The following example:
int[] arg = {6,3,8,2,9,1}
The first trip 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
A first pass total of 3 times 5 times Comparative exchange, 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 exchange 2, sort 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 comparisons conducted a total of 3 exchange 2, sort results: 231,689
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 to exchange 1, sort 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 to exchange 1, sort results: 123,689
The end result: 123 689
Thus, N (arg.length) to sort digital completed, a total of N-1 times need to 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 inner control cycles per trip
for(int i = 0; i<arg.length-1; i++) { for(int y = 0; y<arg.length-i-1; y++) { } }
Bubble sort of advantage: every trip to be sorted, a comparison will be less, because every sort will be a trip to find a great value. Example above: after the first pass compared to the last row of a certain number is a maximum number, the second pass when sorting, among others need only compare the number of a number other than the last, you can also find a maximum the number of rows in the back several times to participate in the second comparison, when comparing the third trip, need only compare the number in addition to other than the last two numbers, and so on ...... in other words, did not carry out a trip to compare every a trip to a relatively small, to some extent, reduce the amount of the algorithm.
Code:
public class Base { public static void main(String[] arg) { int[] arr = {10, 5, 8, 6, 3, 7, 2, 6, 9}; bubbleSort(arr); for (int i = 0; i < arr .length; i++) { System.out.print(arr[i]); } } public static void bubbleSort(int[] arg ) { for (int i = 0; i < arg.length - 1; i++) {//控制循环次数(N-1) for (int y = 0; y < arg.length - i-1; y++) { if (arg [and]> arg [y + 1 ]) { int temp = arg [and]; arg [y] = arg [y + 1 ]; arg [y + 1] = temp; } } } } }