冒泡排序 (Bubble sort)

原文:http://en.wikipedia.org/wiki/Bubble_sort


比较两个相邻的元素,后面元素小于前面元素则交换二者位置。

An example of bubble sort. Starting from the beginning of the list, compare every adjacent pair, swap their position if they are not in the right order (the latter one is smaller than the former one). After each iteration, one less element (the last one) is needed to be compared until there are no more elements left to be compared.
伪代码:
procedure bubbleSort( A : list of sortable items )
   repeat     
     swapped = false
     for i = 1 to length(A) - 1 inclusive do:
       /* if this pair is out of order */
       if A[i-1] > A[i] then
         /* swap them and remember something changed */
         swap( A[i-1], A[i] )
         swapped = true
       end if
     end for
   until not swapped
end procedure


你可能感兴趣的:(冒泡排序 (Bubble sort))