排序——冒泡排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了排序——冒泡排序相关的知识,希望对你有一定的参考价值。

【冒泡排序】

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

  对于给定的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] + " ");                                                                                                                  

  }                                                                                                                                                      

}  

 

以上是关于排序——冒泡排序的主要内容,如果未能解决你的问题,请参考以下文章

nodejs实现冒泡排序和快速排序

冒泡排序算法有几种写法?

冒泡排序

拓扑排序(图)、冒泡排序、插入排序

深度解析(十五)冒泡排序

冒泡排序