python快速排序、堆排序、计数排序、桶排序、基数排序_一只什么都不懂的码农的博客
常用排序算法总结和对比_玖玖拾月陆的博客-CSDN博客_各种排序算法的总结和比较
#快速排序(自己做过优化的结果)
import random
def quickSort(arr, head,tail):
if head >= tail:
return arr
mid = head + random.randint(0,tail - head)
pivot = arr[mid]
arr[mid] = arr[head]
low = head
high = tail
while low != high:
while low < high and arr[high] >= pivot:
high -= 1
arr[low] = arr[high]
while low < high and arr[low] <= pivot:
low += 1
arr[high] = arr[low]
arr[low] = pivot
quickSort(arr, head, low - 1)
quickSort(arr, low + 1, tail)
return arr
if __name__ == '__main__':
arr = list(map(int,input().strip().split()))
kk = quickSort(arr,0,len(arr)-1)
print(kk)
#堆排序
def buildMaxHeap(arr):
import math
for i in range(math.floor(len(arr)/2),-1,-1):
heapify(arr,i)
def heapify(arr, i):
min1 = 2*i+1
max1 = 2*i+2
now = i
if min1 < leng and arr[min1] > arr[now]:
now = min1
if max1 < leng and arr[max1] > arr[now]:
now = max1
if now != i:
swap(arr, i, now)
heapify(arr, now)
def swap(arr, i, j):
arr[i], arr[j] = arr[j], arr[i]
def heapSort(arr):
global leng
leng = len(arr)
buildMaxHeap(arr)
for i in range(len(arr)-1,0,-1):
swap(arr,0,i)
leng -=1
heapify(arr, 0)
return arr
arr = [3,44,38,5,47,15,36,26,27,2,46,4,19,50,48]
print(heapSort(arr))
标签:arr,now,堆排序,head,high,low,排序,快速
From: https://www.cnblogs.com/chuqianyu/p/16583865.html