1 //双循环 2 public static void bubbleSort(int[] arr) 3 { 4 for(int i=arr.length-1; i>0; --i) 5 for(int j=0; j<i; ++j) 6 if(arr[j]>arr[j+1]) 7 { 8 int temp=arr[j]; 9 arr[j]=arr[j+1]; 10 arr[j+1]=temp; 11 } 12 } 13 14 //单循环 15 public static void bubbleSort(int[] arr) 16 { 17 int end=arr.length-1; 18 for(int i=0; i<end; ++i) 19 { 20 if(arr[i]>arr[i+1]) 21 { 22 int temp=arr[i]; 23 arr[i]=arr[i+1]; 24 arr[i+1]=temp; 25 } 26 if(i==end-1) 27 { 28 i=-1; 29 --end; 30 } 31 } 32 }