The Java bubbling algorithm is one of the most common algorithms. So what is a bubbling algorithm? As shown (image from the network):
In fact, you can see that the first pass through the process, first compare the first pair of numbers, compared to the completion of the second number must be relatively large, then the second pair, the same comparison exchange, then this time the third number must be larger, so that all the number of comparison completed, The largest number bubbles to the far right of the array. In the second round you can put the second largest number in the second position on the right, and so on ... When you traverse a number that is not interchangeable, it means that the queue is already in order.
1 Public Static voidMain (string[] args) {2 int[] arr = {11, 13, 10, 34, 31, 24, 15, 16, 26, 9, 16, 4992, 356};3 Bubblesort (arr);//Sort complete, number is 9,10,11,13,15,16,16,24,26,31,34,356,4992,4 }5 6 7 Public Static voidBubblesort (int[] arr) {8 for(inti = 0; i < arr.length-1; i++) {9 BooleanFlag =true;//setting a token, if true, indicates that the sort has already been completed. Ten for(intj = 0; J < arr.length-1-I; J + +) { One if(Arr[j] > arr[j + 1]) { A inttemp =Arr[j]; -ARR[J] = arr[j+1]; -ARR[J+1] =temp; theFlag =false; - } - } - if(flag) { +System.out.print ("Sort done, number is"); - for(intI1:arr) { +System.out.print (i1+ ","); A } at Break; - } - } - -}
Algorithm (1)--java bubbling algorithm