I tried to write the practice of using the class in a way that I could think of, but is this really O (log n)? (The story of looking at the calculation time properly). It should come out in ascending order.
I'm using python standard list operations here and there, so unfortunately it becomes O (n) there.
def heapsort_one_step(heaplist,child_pos):
if child_pos == 0:
return heaplist,0,-1
child = heaplist[child_pos]
parent_pos = (child_pos-1)//2
parent = heaplist[parent_pos]
if parent > child:
heaplist[child_pos], heaplist[parent_pos] = parent,child
return heaplist,parent_pos,1
else:
return heaplist,parent_pos,-1
def heapsort(heaplist):
a = 1
child_pos = len(heaplist)-1
while a != -1:
heaplist,child_pos,a = heapsort_one_step(heaplist,child_pos)
return heaplist
def reconstruct_heap_one_step(heaplist,parent_pos):
if len(heaplist) == 2:
heaplist = [min(heaplist),max(heaplist)]
return heaplist, parent_pos, -1
if 2*parent_pos+2 > len(heaplist)-1:
return heaplist, parent_pos,-1
child1 = heaplist[2*parent_pos + 1]
child2 = heaplist[2*parent_pos + 2]
if child1 > child2:
minchild = child2
child_pos = 2*parent_pos + 2
else:
minchild = child1
child_pos = 2*parent_pos + 1
if heaplist[parent_pos] > minchild:
heaplist[child_pos], heaplist[parent_pos] = heaplist[parent_pos],heaplist[child_pos]
return heaplist, child_pos,1
else:
return heaplist,child_pos,-1
def reconstruct_heap(heaplist):
a = 1
parent_pos = 0
while a != -1:
heaplist, parent_pos, a = reconstruct_heap_one_step(heaplist,parent_pos)
return heaplist
class Heapq:
def __init__(self,data = []):
self.data = data
def push(self,x):
self.data.append(x)
self.data = heapsort(self.data)
print(self.data)
def top(self):
return self.data[0]
def pop(self):
if len(self.data) !=0:
popval= self.data[0]
self.data[0] = self.data[-1]
self.data = self.data[:-1]
self.data = reconstruct_heap(self.data)
return popval
else:
print("Queue is empty.")
queue = []
x = Heapq(queue)
import random
for i in range(10):
x.push(random.randint(0,100))
print(x.data)
for i in range(10):
print(x.pop())
I feel that there is definitely a more concise way of writing.
(Addition) It seems that the default variable that was put in \ _ \ _ init \ _ \ __ for some reason was potentially very dangerous (see comment).
Migrated to python3.6 with Virtual Box + Ubuntu + Anaconda.
Recommended Posts