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?
Assuming your
arrayHeapprovides the same guarantees as the stdlib'sheapqor 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:
With a slightly nicer API, like the one in the stdlib's
heapqmodule (is there a reason you're not using it, by the way?), you can make this even simpler:… or, if you're sure
lstis a list and you don't mind mutating it:… or, of course, you can copy
lstand then mutate the copy:You may notice that the first one is the first of the Basic Examples in the
heapqdocs.