First I wrote the first sample of code and it didn't work correctly. I prefer the first sample, but only the second one works correctly. I don't know why the first sample doesn't change the original array but second does. Where is the difference?
First sample:
import heapq
def heap_sort(tab):
heap = []
for i in tab:
heapq.heappush(heap, i)
tab = [heapq.heappop(heap) for _ in xrange(len(heap))]
temp_tab = [4, 3, 5, 1]
heap_sort(temp_tab)
print temp_tab
Prints:
[4, 3, 5, 1]
Second sample:
import heapq
def heap_sort(tab):
heap = []
for i in tab:
heapq.heappush(heap, i)
for i, _ in enumerate(tab):
tab[i] = heapq.heappop(heap)
temp_tab = [4, 3, 5, 1]
heap_sort(temp_tab)
print temp_tab
Prints:
[1, 3, 4, 5]