Package WZS. sort; import Java. util. arrays; // Bubble Sorting // basic idea: In the number of groups to be sorted, all the numbers within the range not sorted yet, // compare and adjust the adjacent two numbers from top to bottom, so that the larger numbers will sink, and the smaller numbers will go up. // That is, when the numbers of two adjacent parties are compared and their sorting and sorting requirements are the opposite, they are exchanged. // 54321 // start bubbling... // 45321 // 43521 // 43251 // 43215 // the end of the first round... // 34215 // 32415 // 32145 // the end of the second round... // 23145 // 21345 // the end of the third round... // 12345 // the end of the fourth round... public class test_wzs001 {public static void main (string [] ARGs) {int [] intarray = {10, 9, 5, 3, 6, 4, 7, 8, 2, 1}; bubblesort (intarray);}/*** bubble sort * @ Param intarray array to be sorted */static void bubblesort (INT [] intarray) {int temp; // I indicates the number of comparisons for (INT I = 0; I <intarray. length-1; I ++) {// J indicates the number of comparisons for (Int J = 0; j <intarray. length-1-I; j ++) {If (intarray [J]> intarray [J + 1]) {temp = intarray [J]; intarray [J] = intarray [J + 1]; intarray [J + 1] = temp;} system. out. println ("Number" + (I + 1) + "sort:" + arrays. tostring (intarray);} system. out. println ("\ n sorting result:" + arrays. tostring (intarray ));}}
Output result:
1st sorting: [9, 5, 3, 6, 4, 7, 8, 2, 1, 10] 2nd sorting: [5, 3, 6, 4, 7, 8, 2, 1, 9, 10] 3rd sorting: [3, 5, 4, 6, 7, 2, 1, 8, 9, 10] 4th sorting: [3, 4, 5, 6, 2, 1, 7, 8, 9, 10] 5th sorting: [3, 4, 5, 2, 1, 6, 7, 8, 9, 10] 6th sorting: [3, 4, 2, 1, 5, 6, 7, 8, 9, 10] 7th sorting: [3, 2, 1, 4, 5, 6, 7, 8, 9, 10] 8th sorting: [2, 1, 3, 4, 5, 6, 7, 8, 9, 10] 9th sorting: [1, result of sorting 2, 3, 4, 5, 6, 7, 8, 9, 10: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]