冒泡排序算法
①基本思想:兩個數比較大小,較大的數下沉,較小的數冒起來。spa
②算法描述:code
③動圖演示(來源參考資料)orm
④代碼實現blog
1 public class maoPaoDemo{ 2 public static void main(String[] args){ 3 int[] arr = {12,5,7,9,10,70,56,2,105,6}; 4 for(int i=0;i<arr.length-1;i++){ 5 for(int j=0;j<arr.length-i-1;j++){ 6 if(arr[j]>arr[j+1]){ 7 int temp=0; 8 temp=arr[j]; 9 arr[j]=arr[j+1]; 10 arr[j+1]=temp; 11 } 12 } 13 } 14 for(int i=0;i<arr.length;i++){ 15 System.out.print(arr[i]+" "); 16 } 17 } 18 }