0

I'm trying to do the following problem

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')

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.

The traceback -

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
4
  • Can you post the traceback? Commented Dec 2, 2013 at 21:43
  • 1
    Replace [2] with 2. It makes no sense to take the log with respect to a list ;-) Commented Dec 2, 2013 at 21:45
  • the way you use this limit is weird. It should be computed once on the complete list, then a counter should be incremented at each level of recursion and compared to that value. Commented Dec 2, 2013 at 21:46
  • what you are doing is not even recursive, btw Commented Dec 2, 2013 at 21:47

1 Answer 1

2
        limit = float(log(len(lst),[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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.