临近的数字两两进行比较,按照从小到大或者从大到小的顺序进行交换,

最大或最小的数字被交换到了最后一位,

int[] array = new int[]{10,11,9,8,15,1,7,10,13,20}; 

 

public void bubbleSort(int[] array){
  int temp;
   for(int i = 0;i < array.length; i++){
     for(int j = array.length - 1; j > i;j--){
        if(array[j] < array[j-1]){
           temp = array[j];
             array[j] = array[j-1];
             array[j-1] = temp;
           }
       }
    }
}

  

                                                                                                                                                                                                                                                                                     

posted on 2014-01-17 20:00  ✿ 小塔  阅读(135)  评论(0编辑  收藏  举报