1 Public classBubblesorttest {2 //Bubble Sort3 Public Static voidBubblesort (int[] source) {4 //the outer loop controls the number of traversal times, the number of n is sorted, and the n-1 is traversed5 for(inti = source.length-1; i > 0; i--) {6 //each traversal is completed, the element labeled I is determined, and the next traversal is no longer participating in the comparison7 for(intj = 0; J < I; J + +) {8 if(Source[j] > source[j + 1]) {9Swap (source, J, J + 1);Ten } One } A } - } - //private completion of the SWAP function sub-function the Private Static voidSwapint[] Source,intXinty) { - inttemp =Source[x]; -SOURCE[X] =Source[y]; -Source[y] =temp; + } - //Test in main + Public Static voidMain (string[] args) { A int[] A = {4, 2, 1, 6, 3, 6, 0,-5, 1, 1}; at - Bubblesort (a); - //Local variables to initialize - for(inti = 0; i < a.length; i++) { - //format output with printf -System.out.printf ("%d", A[i]); in } - } to}
Bubble sort (Java edition)