问题描述
我正在尝试解决以下问题
I'm trying to do the following problem
目的:quicheSort算法的实现(不到位), 它首先使用quickSort,使用3位中值枢轴,直到它 达到以int(math.log(N,2))为边界的递归限制. 在此,N是要排序的初始列表的长度. 一旦达到该深度限制,它将切换为使用heapSort而不是 快速排序.
purpose: Implementation of the quicheSort algorithm (not in place), It first uses quickSort, using the median-of-3 pivot, until it reaches a recursion limit bounded by int(math.log(N,2)). Here, N is the length of the initial list to sort. Once it reaches that depth limit, it switches to using heapSort instead of quicksort.
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.
The problem i'm having with this code is my limits. I'm supposed to set the limit as int(log(N,[2])). However, python keeps telling me a float is needed. So when I change int to float it still keeps telling me a float is needed.
回溯-
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
推荐答案
limit = float(log(len(lst),[2]))
[2]
是一个1元素的列表.为什么要列出1个元素的清单?您只想在这里2
.我认为也许这应该是地板的数学符号,但是2楼也没有太大意义.
[2]
is a 1-element list. Why are you making a 1-element list? You just want 2
here. I'd think maybe that was supposed to be mathematical notation for a floor, but flooring 2 doesn't make much sense either.
这篇关于排序python堆和列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!