1. 程式人生 > 實用技巧 >演算法_python各種排序的程式碼實現

演算法_python各種排序的程式碼實現

python各種排序的程式碼實現

氣泡排序

# 氣泡排序
def bubbleSort(alist):
    for passnum in range(len(alist)-1, 0, -1):
        for i in range(passnum):
            if alist[i] > alist[i + 1]:
                temp = alist[i]
                alist[i] = alist[i + 1]
                alist[i + 1] = temp
    return alist

選擇排序

# 選擇排序
def selectSort(alist):
    for fillslot in range(len(alist) - 1, 0, -1):
        positionOfMax = 0 
        for location in range(1, fillslot + 1):
            if alist[location] > alist[positionOfMax]:
                positionOfMax = location
            if positionOfMax != fillslot:
                alist[fillslot], alist[positionOfMax] = alist[positionOfMax], alist[fillslot]
    return alist            
    
alist = [4, 8, 1, 9, 2, 0, 3, 7, 5, 6]
print(selectSort(alist))

插入排序

# 插入排序
def insertSort(alist):
    for index in range(len(alist)):
        currentvalue = alist[index]
        position = index
        
        while position > 0 and alist[position - 1] > currentvalue:
            alist[position] = alist[position - 1]
            position = position - 1
        alist[position] = currentvalue
        
    return alist

alist = [4, 8, 1, 9, 2, 0, 3, 7, 5, 6]
print(insertionSort(alist))

希爾排序

# 希爾排序
def shellSort(alist):
    sublistcount = len(alist) // 2
    while sublistcount > 0:
        for startposition in range(sublistcount):
            gapinsertionSort(alist, startposition, sublistcount)
            print("After increments of size", sublistcount, "Then list is", alist)
        sublistcount = sublistcount // 2
    return alist

def gapinsertionSort(alist, start, gap):
    for i in range(start+gap, len(alist), gap):
        currentvalue = alist[i]
        position = i
        
        while position >= gap and alist[position - gap] > currentvalue:
            alist[position] = alist[position - gap]
            position = position - gap
        alist[position] = currentvalue

alist = [4, 8, 1, 9, 2, 0, 3, 7, 5, 6]
print(shellSort(alist))

歸併排序

# 歸併排序--遞迴實現
def mergeSort(alist=[]):
    if len(alist) <= 1:
        return alist
    else:
        mid = len(alist) // 2
        left = []
        right = []
        left = mergeSort(alist[:mid])
        right = mergeSort(alist[mid:])
        return merge(left, right)
        
def merge(left=[], right=[]):
    # i, j are index for left and right seperately
    i, j = 0, 0
    result = []
    while i < len(left) and j < len(right):
        if left[i] <= right[j]:
            result.append(left[i])
            i += 1
        else:
            result.append(right[j])
            j += 1
    
    # 將剩餘部分依次加入 result
    result = result + left[i:]
    result = result + right[j:]
    return result

alist = [4, 8, 1, 9, 2, 0, 3, 7, 5, 6]
print(mergeSort(alist))

快速排序

# 快速排序
def quicksort(collection):
    length = len(collection)
    if length <= 1:
        return collection
    else:
        # Use the last element as the first pivot
        pivot = collection.pop()
        # Put elements greater than pivot in greater list
        # Put elements lesser than pivot in lesser list
        greater, lesser = [], []
        for element in collection:
            if element > pivot:
                greater.append(element)
            else:
                lesser.append(element)
        return quicksort(lesser) + [pivot] + quicksort(greater)


if __name__ == '__main__':
    list1 = [7, 3, 23, 6, 9, 9, 10]
    res = quicksort(list1)
    print(res)