First, what is bubble sort: the bubbling sort is compared between two adjacent numbers, where the previous value is defined as before, and the following value is defined as after; when Before>after I, exchange their values, if before<after, do not swap.
The time complexity of bubble sort is indicated by the large O method: O (n^2), low computational efficiency, but the algorithm idea is simple, it is a sort method of comparison basis
Bubble sort Idea Diagram:
Bubble Sort Reference Code:
Public classPopsort {int[] Array =NULL; @Test Public voidTestpopsort () {array=New int[5]; array[0] = 45; array[1] = 22; array[2] = 33; array[3] = 43; array[4] = 34; intNelmes =Array.Length; for(inti = nElmes-1; i > 1; i--) { for(intj = 0; J < I; J + +) { if(Array[j] > array[j + 1]) {swap (J, J+ 1); }}} System.out.println (Arrays.tostring (array)); } Private voidSwapintOneintBoth ) {Integer temp=Array[one]; Array[one]=Array[two]; Array[two]=temp; }}
Java structure and algorithm bubble sort