算法步骤如图:
public class Test_BubbleSort { public static void main(String[] args) { int a[] = {3,4,1,7,8,12,16,73,33,85,452,421,554,12,34,78,64,9,4,66,42}; BubbleSort(a); for(int i=0;i<a.length-1;i++){ System.out.println(a[i]); } } public static void BubbleSort(int[] a){ if(a.length==0||a.length<2){ return; } else{ for(int i=a.length-1;i>0;i--){ for(int j=0;j<i-1;j++){ if(a[j]>a[j+1]){ Swap(a,j,j+1); } } } } } public static void Swap(int[] a,int i,int j){ // int temp; // temp = a[i]; // a[i] = a[MinIndex]; // a[MinIndex] = temp; a[i] = a[i] ^ a[j]; a[j] = a[i] ^ a[j]; a[i] = a[i] ^ a[j]; } }