解决方案 »

  1.   

    publicclassBubbleSort { 
    publicvoidsort(int[]a) { 
    inttemp=0; 
    for(inti=a.length-1;i>0;--i) 

    for(intj=0;j<i;++j) 

    if(a[j+1]<a[j]) 

    temp=a[j]; 
    a[j]=a[j+1]; 
    a[j+1]=temp; 




      

  2.   

    public class BubbleSort {
    public void Sort(int[] targetArr){
    //小到大的排序           
    int temp = 0;            
    for(int i = 0;i<targetArr.length;i++)
    {                
    for(int j = i;j<targetArr.length;j++)
    {                    
    if(targetArr[i]>targetArr[j])
    {                       
    //方法一:                        
     temp = targetArr[i];                        
     targetArr[i] = targetArr[j];                       
     targetArr[j] = temp; 
     //方法二:
    /* targetArr[i] = targetArr[i] + targetArr[j];                        
     targetArr[j] = targetArr[i] - targetArr[j];                        
     targetArr[i] = targetArr[i] - targetArr[j];*/
     } 
    }
    }
    }
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    BubbleSort bs = new BubbleSort();            
     int[] arr  = new int[]{45,22,11,7,400,99,20,67,5};    
     bs.Sort(arr);            
     for(int i : arr)
     {                

    System.out.print(i+",");           
     }
    }
    }