Java Bubble sorting and java Bubble Sorting
First paste out the code (sorted from small to large ):
Public class BubbleSort {public static void main (String args []) {double [] a = {0.4, -2.5}; for (int I = 0; I <. length-1; I ++) {// Number of sorted orders in the outer loop control for (int j = 0; j <. length-i-1; j ++) {// The inner loop controls how many times each trip is sorted if (a [j]> a [j + 1]) {// double num = a [j]; a [j] = a [j + 1]; // swap the large value to the end of a [j + 1] = num; // swap the small value to the end} for (double k:) {// foreach cyclically outputs System. out. println (k);} // for (int k = 0; k <. length; k ++) {// common for loop // System. out. println (a [k]); //}
Note: To arrange the values from large to small, modify the if statement to be greater than the symbol.
Annotation: a. length-1 and j <a. length-i-1
Example: to sort the array: int [] arr = {6, 3, 8, 2, 9, 1}; (annotation Source: http://www.bkjia.com/article/68204.htm)
First sort: (I)
First sorting: Comparison between 6 and 3, where 6 is greater than 3, switching position: 3 6 8 2 9 1
Second sorting: Comparison of 6 and 8, where 6 is smaller than 8, without switching position: 3 6 8 2 9 1
Third sorting: Comparison between 8 and 2, where 8 is greater than 2, switching position: 3 6 2 8 9 1
Fourth sorting: Comparison between 8 and 9, where 8 is smaller than 9, without switching position: 3 6 2 8 9 1
Fifth sorting: 9 and 1 Comparison: 9 greater than 1, switching location: 3 6 2 8 1 9
In the first round, a total of 5 (j) comparisons were conducted. The sorting results are as follows: 3 6 2 8 1 9
Second sort: (I)
First sorting: 3 and 6 comparison, 3 less than 6, not swap location: 3 6 2 8 1 9
Second sorting: Comparison between 6 and 2, where 6 is greater than 2, switching position: 3 2 6 8 1 9
Third sorting: Comparison of 6 and 8, where 6 is greater than 8, without switching position: 3 2 6 8 1 9
Fourth sorting: Comparison of 8 and 1, where 8 is greater than 1, switching position: 3 2 6 1 8 9
In the second round, a total of 4 (j) comparisons were conducted. The sorting results are as follows: 3 2 6 1 8 9
Third sort: (I)
First sorting: 3 and 2 Comparison, 3 greater than 2, switch location: 2 3 6 1 8 9
Second sorting: 3 and 6 comparison, 3 less than 6, not swap location: 2 3 6 1 8 9
Third sorting: Comparison of 6 and 1, where 6 is greater than 1, switching position: 2 3 1 6 8 9
A total of 3 (j) comparisons were conducted for the second trip. The sorting result is: 2 3 1 6 8 9.
Fourth sort: (I)
First sorting: 2 and 3 comparison, 2 less than 3, not swap location: 2 3 1 6 8 9
Second sorting: 3 and 1 comparison, 3 greater than 1, switch location: 2 1 3 6 8 9
A total of 2 (j) comparisons were conducted for the second trip. The sorting result is: 2 1 3 6 8 9.
Fifth sort: (I)
First sort: 2 and 1 compare, 2 is greater than 1, switch location: 1 2 3 6 8 9
A total of 1 (j) comparisons were conducted for the second trip. The sorting result is: 1 2 3 6 8 9.
Final Result: 1 2 3 6 8 9