冒泡排序(bubblesort)

伪码:

BUBBLESORT(A)
  for i ← 1 to length[A]
    do for j ← length[A] downto i + 1
      do if A[j] < A[j - 1]
        then exchange A[j] ↔ A[j - 1]

Java 实现

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

 

 

 

posted @ 2012-05-06 09:57  qiangzhu  阅读(209)  评论(0编辑  收藏  举报