def quick_sort(array): if len(array) <=1: # 递归跳出条件 returnarray pivot =array[0] left= [i for i inarray[1:] if i < pivot] right= [i for i inarray[1:] if i >= pivot] return quick_sort(left) + [pivot] + quick_sort(right)
冒泡排序
1 2 3 4 5 6
def bubble_sort(array): for i in range(len(array) - 1): for j in range(len(array) - i -1): # 已排序好的部分不需再遍历 ifarray[j] > array[j+1]: array[j], array[j+1] = array[j+1], array[j] returnarray
桶排
1 2 3 4 5 6 7 8 9 10
def bucker_sort(array): result= [] minVal, maxVal =min(array), max(array) bucket = [0] * (maxVal - minVal +1) # 所需的桶数 for i inarray: bucket[i - minVal] +=1 # 每个数字出现的次数 for i inrange(len(bucket)): if bucket[i]: result+= [i + minVal] * bucket[i] returnresult