Python 同时使用堆排序和快速排序
我必须同时使用堆排序和快速排序,这样当递归深度超过原始列表大小的日志基数2时,它就会切换到堆排序实现 我的堆排序函数:Python 同时使用堆排序和快速排序,python,python-3.x,Python,Python 3.x,我必须同时使用堆排序和快速排序,这样当递归深度超过原始列表大小的日志基数2时,它就会切换到堆排序实现 我的堆排序函数: import heapq def heapSort(lst): """ heapSort(List(Orderable)) -> List(Ordered) performs a heapsort on 'lst' returning a new sorted list Postcondition: the argument ls
import heapq
def heapSort(lst):
"""
heapSort(List(Orderable)) -> List(Ordered)
performs a heapsort on 'lst' returning a new sorted list
Postcondition: the argument lst is not modified
"""
heap = []
for item in lst:
heapq.heappush(heap, item)
sortedlist = []
while len(heap) > 0:
sortedlist.append(heapq.heappop(heap))
return sortedlist
def medianOf3(lst):
"""
From a lst of unordered data, find and return the the median value from
the first, middle and last values.
"""
a,b,c = lst[0], lst[len(lst)//2], lst[-1]
return min(min(max(a,b), max(b,c)), max(a,c))
def quickSort(lst):
"""
quickSort: List(lst) -> List(result)
Where the return 'result' is a totally ordered 'lst'.
It uses the median-of-3 to select the pivot
e.g. quickSort([1,8,5,3,4]) == [1,3,4,5,8]
"""
if lst == []:
return []
else:
pivot = medianOf3(lst)
less, same, more = partition(pivot, lst)
return quickSort(less) + same + quickSort(more)
def partition( pivot, lst ):
"""
partition: pivot (element in lst) * List(lst) ->
tuple(List(less), List(same, List(more))).
Where:
List(Less) has values less than the pivot
List(same) has pivot value/s, and
List(more) has values greater than the pivot
e.g. partition(5, [11,4,7,2,5,9,3]) == [4,2,3], [5], [11,7,9]
"""
less, same, more = list(), list(), list()
for val in lst:
if val < pivot:
less.append(val)
elif val > pivot:
more.append(val)
else:
same.append(val)
return less, same, more
我的快速排序功能:
import heapq
def heapSort(lst):
"""
heapSort(List(Orderable)) -> List(Ordered)
performs a heapsort on 'lst' returning a new sorted list
Postcondition: the argument lst is not modified
"""
heap = []
for item in lst:
heapq.heappush(heap, item)
sortedlist = []
while len(heap) > 0:
sortedlist.append(heapq.heappop(heap))
return sortedlist
def medianOf3(lst):
"""
From a lst of unordered data, find and return the the median value from
the first, middle and last values.
"""
a,b,c = lst[0], lst[len(lst)//2], lst[-1]
return min(min(max(a,b), max(b,c)), max(a,c))
def quickSort(lst):
"""
quickSort: List(lst) -> List(result)
Where the return 'result' is a totally ordered 'lst'.
It uses the median-of-3 to select the pivot
e.g. quickSort([1,8,5,3,4]) == [1,3,4,5,8]
"""
if lst == []:
return []
else:
pivot = medianOf3(lst)
less, same, more = partition(pivot, lst)
return quickSort(less) + same + quickSort(more)
def partition( pivot, lst ):
"""
partition: pivot (element in lst) * List(lst) ->
tuple(List(less), List(same, List(more))).
Where:
List(Less) has values less than the pivot
List(same) has pivot value/s, and
List(more) has values greater than the pivot
e.g. partition(5, [11,4,7,2,5,9,3]) == [4,2,3], [5], [11,7,9]
"""
less, same, more = list(), list(), list()
for val in lst:
if val < pivot:
less.append(val)
elif val > pivot:
more.append(val)
else:
same.append(val)
return less, same, more
def medianOf3(lst):
"""
从无序数据的lst中,查找并返回
第一个、中间和最后一个值。
"""
a、 b,c=lst[0],lst[len(lst)//2],lst[-1]
返回最小值(最小值(最大值(a,b),最大值(b,c)),最大值(a,c))
def快速排序(lst):
"""
快速排序:列表(lst)->列表(结果)
其中返回的“result”是完全有序的“lst”。
它使用3的中间值来选择轴
e、 g.快速排序([1,8,5,3,4])==[1,3,4,5,8]
"""
如果lst=[]:
返回[]
其他:
枢轴=中间层3(lst)
更少、相同、更多=分区(枢轴、lst)
返回快速排序(更少)+相同+快速排序(更多)
def分区(枢轴,lst):
"""
分区:轴(lst中的元素)*列表(lst)->
元组(列表(更少)、列表(相同、列表(更多)))。
哪里:
列表(较少)的值小于轴
列表(相同)具有轴值,并且
列表(更多)的值大于轴
e、 g.划分(5,[11,4,7,2,5,9,3])==[4,2,3],[5],[11,7,9]
"""
less,same,more=list(),list(),list()
对于lst中的val:
如果val<枢轴:
减.追加(val)
elif val>枢轴:
更多。附加(val)
其他:
相同。追加(val)
回报更少、相同、更多
现在我尝试实现quipSort,以便它在堆排序和快速排序之间切换:
def quipSortRec(lst, limit):
"""
A non in-place, depth limited quickSort, using median-of-3 pivot.
Once the limit drops to 0, it uses heapSort instead.
"""
if limit <= 0:
heapSort(lst)
else:
quickSort(lst)
quipSortRec(lst, limit -1)
return lst
def quipSort(lst):
"""
The main routine called to do the sort. It should call the
recursive routine with the correct values in order to perform
the sort
"""
l = math.log2(len(lst))
return quipSortRec(lst, l)
def quipSortRec(lst,极限):
"""
一种不到位、深度有限的快速排序,使用中位数为3的枢轴。
一旦限制降到0,它将使用heapSort。
"""
如果limit你启动了quipSortRec
很好,但是的else
应该看起来像是快速排序的副本,递归调用现在调用quipSortRec
当然了:堆排序+快速排序不应该是“Heck Sort”或“Quap Sort”吗?请比“它不起作用”更具体一些:它应该做什么?它正在做什么?