1. 程式人生 > >Python 演算法 - 氣泡排序

Python 演算法 - 氣泡排序

# coding=utf-8


# 氣泡排序
def bubble_sort(lists):
    """ 
    它重複地走訪過要排序的數列,一次比較兩個元素,如果他們的順序錯誤就把他們交換過來。
    走訪數列的工作是重複地進行直到沒有再需要交換,也就是說該數列已經排序完成。
    """
    count = len(lists)
    for i in range(0, count):
        for j in range(i + 1, count):
            if lists[i] > lists[j]:
                lists[i], lists[j] = lists[j], lists[i]
    return
lists