排序和python堆

2024-04-25 17:00:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着做下面的问题

目的:实现quicheSort算法(未就位), 它首先使用快速排序,使用中间值为3的轴,直到 达到以int为界的递归极限(数学日志(N,2))。 这里,N是要排序的初始列表的长度。 一旦达到这个深度限制,它就会切换到使用堆排序而不是 快速排序。在

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一直告诉我需要一个float。所以当我把int改为float时,它仍然告诉我需要一个float。在

回溯-

^{pr2}$

Tags: thetorunimportforlenreturnif
1条回答
网友
1楼 · 发布于 2024-04-25 17:00:03
        limit = float(log(len(lst),[2]))

[2]是一个单元素列表。你为什么要列一个单元素列表?你只要2在这里。我想也许这应该是地板的数学符号,但是地板2也没有什么意义。在

相关问题 更多 >