排序python堆和列表
我正在尝试做下面的问题 目的:实施quicheSort算法(未到位), 它首先使用快速排序,使用中位数为3的轴,直到 达到以int(math.log(N,2))为界的递归限制。 这里,N是要排序的初始列表的长度。 一旦达到深度限制,它将切换到使用heapSort而不是 快速排序排序python堆和列表,python,list,sorting,heap,Python,List,Sorting,Heap,我正在尝试做下面的问题 目的:实施quicheSort算法(未到位), 它首先使用快速排序,使用中位数为3的轴,直到 达到以int(math.log(N,2))为界的递归限制。 这里,N是要排序的初始列表的长度。 一旦达到深度限制,它将切换到使用heapSort而不是 快速排序 import heapSort # heapSort import qsPivotMedian3 from math import* # log2 (for quic
import heapSort # heapSort
import qsPivotMedian3
from math import* # log2 (for quicksort depth limit)
import testSorts # run (for individual test run)
def quicheSortRec(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 len(lst) == 0:
return lst()
elif limit > 0:
quickSort(lst)
else:
heapSort(lst)
def quicheSort(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
"""
if len(lst)== 0:
return list()
else:
limit = float(log(len(lst),[2]))
return quicheSortRec(lst,limit)
if __name__ == "__main__":
testSorts.run('quicheSort')
这个代码的问题是我的极限。我应该将限制设置为int(log(N,[2])。然而,python一直告诉我需要一个浮点。所以当我把int改为float时,它仍然告诉我需要一个float
回溯-
le "/Users/sps329/Desktop/quicheSort.py", line 44, in <module>
testSorts.run('quicheSort')
File "/Users/sps329/Desktop/testSorts.py", line 105, in run
performSort(sortType, data, N)
File "/Users/sps329/Desktop/testSorts.py", line 71, in performSort
result = sortType.function(dataSet.data)
File "/Users/sps329/Desktop/quicheSort.py", line 40, in quicheSort
limit = float(log(len(lst),[2]))
TypeError: a float is required
le”/Users/sps329/Desktop/quicheSort.py“,第44行,在
testSorts.run('quicheSort')
文件“/Users/sps329/Desktop/testSorts.py”,第105行,正在运行
performSort(排序类型,数据,N)
performSort中第71行的文件“/Users/sps329/Desktop/testSorts.py”
结果=sortType.function(dataSet.data)
文件“/Users/sps329/Desktop/quicheSort.py”,第40行,在quicheSort中
极限=浮动(对数(长度(lst),[2]))
TypeError:需要浮点
[2]
是一个单元素列表。你为什么要做一个单元素列表?您只需在此处输入2
。我认为这可能是地板的数学符号,但地板2也没有多大意义。你能发布回溯吗?用2
替换[2]
。根据列表获取日志是没有意义的;-)你使用这个限制的方式很奇怪。它应该在完整列表中计算一次,然后在每个递归级别递增一个计数器,并与该值进行比较。顺便说一句,您所做的甚至不是递归的
limit = float(log(len(lst),[2]))