1. 程式人生 > >學習記錄:氣泡排序、選擇排序、快速排序的python實現

學習記錄:氣泡排序、選擇排序、快速排序的python實現

(1)氣泡排序:

def bubbleSort(list):
    if(len(list) == 0):
        return
    for i in range(0,len(list) - 1 ):
        for j in range(0,len(list) - i - 1):
            if list[j + 1] < list[j]:
                list[j],list[j + 1] = list[j+1],list[j]
    return list

(2)選擇排序:

def chooseSort(sorted_list):
    if
(len(sorted_list) == 0): return sorted_list = sorted_list for i in range(0, len(sorted_list) - 1): max = sorted_list[i] index = i for j in range(i + 1, len(sorted_list) - 1): if sorted_list[j] < max: max = sorted_list[j] index = j if
index != i: sorted_list[index], sorted_list[i] = sorted_list[i], sorted_list[index] return sorted_list

(3)快速排序

def quickSort(list):
    if len(list) < 2:
        return list
    else:
        flag = list[0]
        less = [i for i in list[1:] if i <= flag]
        greater = [i for 
i in list[1:] if i > flag] return quickSort(less) + [flag] + quickSort(greater) print (quickSort([12,31,12,3,4,21,213,423,121]))