Ant book in python: Priority queue self-implementation

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

Ant book in python: Priority queue self-implementation
Ant book in python: Sec. 2-5 Graph
Ant book in python: page 42 coin problem
Ant book in python: Sec. 2-4, data structures
Ant book in python: Sec.2-5 Dijkstra's algorithm
Ant book in python: page 43 Interval scheduling
Ant book in python: Sec. 2-5 Graph (preparation)
Ant book in python: page 49 Fence Repair
Queue processing in Python
Ant book in python: Sec. 2-3, Dynamic Programming (DP)
Ant book in python: page 47 Saruman's Army (POJ 3069)
Stack and Queue in Python
Try sorting your own objects with priority queue in Python
Ant book in python: page 45 Dictionary order minimum problem (POJ3617)
I wrote the queue in Python
Review the basics in 1 minute! Python Priority queue for fast minimums
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Implementation module "deque" in queue and Python
Ant book with python (chapter3 intermediate edition ~)
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
Meta-analysis in Python
Unittest in python
I want to create a priority queue that can be updated in Python (2.7)
Epoch in Python
Discord in Python
Python #stack queue
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
Solve "AtCoder version! Ant book (beginner)" with Python!
LiNGAM in Python