def bubble_sort(array): if array: finished = False length = len(array) index = length while not finished: index-=1 finished = True for i in range(index): if array[i]>array[i+1]: temp = array[i+1] array[i+1] = array[i] array[i] = temp finished = False return array def bubble(List):
"""
算法来自 https://zh.wikipedia.org/wiki/%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F
""" for j in range(len(List)-1,1,-1): for i in range(0,j): if List[i]>List[i+1]:List[i],List[i+1]=List[i+1],List[i] return List def test_bubble_sort(): array = [6,5,3,1,8,7,2,4] print bubble(array) if __name__ == "__main__": test_bubble_sort()