冒泡排序 (Bubble sort)

来源:互联网 发布:好看的娱乐圈文 知乎 编辑:程序博客网 时间:2024/05/03 09:14

原文: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 swappedend procedure