0
def heapSort(lst):

    heap = arrayHeap.mkHeap(len(lst), arrayHeap.less)
    alst = list(lst)
    while alst != []:
        v = alst.pop(0)
        arrayHeap.add (heap, v)

    while heap.size != 0:
        w = arrayHeap.removeMin(heap)
        alst.append(w)
    return last

is this a valid heap sort function?

12
  • 1
    Without seeing the code for arrayHeap, nobody can say. Although heapsort is almost always coded in-place, so the most sensible answer is "no". Commented Dec 3, 2013 at 3:32
  • 1
    pop(0) makes your code O(n^2), so no. This is not a working heapsort. Commented Dec 3, 2013 at 3:38
  • well this is not a in-place heap sort. and the array heap function is pretty long.... Commented Dec 3, 2013 at 3:40
  • Also, you have a return last instead of return alst, so it should have been obvious that this doesn't work if you tried it. Commented Dec 3, 2013 at 3:40
  • 1
    I get the feeling this is a homework assignment written by someone who'd rather be teaching Scheme, and who hasn't bothered to explain the massive differences between Python lists (variable-length arrays, cheap to insert/delete on the right, expensive on the left, and there are better ways to iterate than popping from the right) and Lisp lists (linked lists, cheap to insert/delete on the left, expensive on the right, and repeated cdr'ing is the best way to iterate). Commented Dec 3, 2013 at 3:54

1 Answer 1

1

Assuming your arrayHeap provides the same guarantees as the stdlib's heapq or any other reasonable heap implementation, then this is a valid heap sort, but it's a very silly one.

By copying the original sequence into a list and then popping from the left side, you're doing O(N^2) preparation for your O(N log N) sort.

If you change this to pop from the right side, then you're only doing O(N) preparation, so the whole thing takes O(N log N), as a heapsort should.

That being said, I can't understand why you want to pop off the list instead of just iterating over it. Or, for that matter, why you want to copy the original sequence into a list instead of just iterating over it directly. If you do that, it will be faster, and use only half the memory, and be much simpler code. Like this:

def heapSort(lst):
    heap = arrayHeap.mkHeap(len(lst), arrayHeap.less)
    for v in lst:
        arrayHeap.add(heap, v)
    alst = []
    while heap.size:
        w = arrayHeap.removeMin(heap)
        alst.append(w)
    return last

With a slightly nicer API, like the one in the stdlib's heapq module (is there a reason you're not using it, by the way?), you can make this even simpler:

def heapSort(lst):
    alst = []
    for v in lst:
        heapq.heappush(alst, v)
    return [heapq.heappop(alst) for i in range(len(alst))]

… or, if you're sure lst is a list and you don't mind mutating it:

def heapSort(lst):
    heapq.heapify(lst)
    return [heapq.heappop(lst) for i in range(len(lst))]

… or, of course, you can copy lst and then mutate the copy:

def heapSort(lst):
    alst = lst[:]
    heapq.heapify(alst)
    return [heapq.heappop(alst) for i in range(len(alst))]

You may notice that the first one is the first of the Basic Examples in the heapq docs.

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

7 Comments

well i was required to have a copy.
heapSort.py – where you use the functions in arrayHeap.py to create a heap-sort implementation (Don’t forget that the array returned has to be a copy, and that the original list is unchanged.)
@user3059815: Iterating the input sequence into the heap already makes a copy. You don't have to make a copy so you can copy it.
is there anything that i can enhance for the w?
it is still running very slowly....am i not doing this right? def heapSort(lst): alst = [] for v in lst: heappush(alst, v) return [heapq.heappop(alst) for i in range(len(alst))] while heap.size != 0: w = arrayHeap.removeMin(heap) alst.append(w) return alst if name == "main": testSorts.run('heapSort')
|

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.