首页 > 代码库 > 排序——冒泡排序

排序——冒泡排序

【冒泡排序】

冒泡排序就是整个过程就像气泡一样往上升,单向冒泡排序的基本思想是(假设由小到大排序):

  对于给定的n个记录,从第一个记录开始依次对相邻的两个记录进行比较,当前面的记录大于后面的记录时,交换位置,进行一轮比较和换位后,n个记录的最大记录将位于第n位;

  然后对前n-1个记录进行第二轮比较;

  重复该过程直到进行比较的记录只剩下一个为止。

 

  冒泡排序(Bubble Sort)是一种交换排序,基本思想是:两两比较相邻记录的关键字,如果反序则交换,直到没有反序的记录为止。

 

Java代码:                                                                                                                                 

public class MaxMin {                                                                                                                                      

public static void BubbleSort(int array[]){                                                                                                            

  int i, j;                                                                                                                                          

  int len = array.length;                                                                                                                            

  int tmp;                                                                                                                                           

                                                                                                                                                   

  for(i=0; i<len-1; ++i)                                                                                                                             

  {                                                                                                                                                  

    for(j=0; j<len-1-i; ++j)                                                                                                                       

    {                                                                                                                                              

      if(array[j] > array[j+1])                                                                                                                  

      {                                                                                                                                          

        tmp = array[j];                                                                                                                        

        array[j] = array[j+1];                                                                                                                 

        array[j+1] = tmp;                                                                                                                      

      }                                                                                                                                          

    }                                                                                                                                              

  }                                                                                                                                                  

}                                                                                                                                                      

  public static void main(String[] args)

  {                                                                                                                

    int a[] = {5, 4, 9, 8, 7, 6, 0, 1, 3, 2};                                                                                                          

    BubbleSort(a);                                                                                                                                     

    for(int i=0; i<a.length; i++)                                                                                                                      

    System.out.print(a[i] + " ");                                                                                                                  

  }                                                                                                                                                      

}  

 

排序——冒泡排序