Algorithm idea: From the back to the adjacent elements of the 22 comparison exchange, each trip will be the smallest or largest element "float" to the end, eventually achieve a completely orderly
PackageSort;ImportFangwei. Directsort; Public classbubblesort{ Public Static voidMain (string[] args) {int[] A =New int[] {20, 30, 90, 40, 70, 110, 60, 10, 100, 50, 80, 1, 2 }; Bubblesort Bubblesort=NewBubblesort (); Bubblesort. Sort (a); ; for(intt:a) System.out.println (t); } voidExchangedata (int[] datas,intIndex1,intindex2) {DATAS[INDEX1]= datas[index1]+Datas[index2]; DATAS[INDEX2]= datas[index1]-Datas[index2]; DATAS[INDEX1]= datas[index1]-Datas[index2]; } Public voidSort (int[] Array) { intn=Array.Length; for(inti = 0; i < n-1; i++)//Number of exchanges: n-1,n-2,n-3 ..... 1 times, note that direct insertion is 1,2,3,4, ... N-1 Times, { for(intj = 0; J<n-i-1; J + +) { if(array[j]>array[j+1]) Exchangedata (Array, J, J+1); } } }}
Sort within-bubble sort