6626070
2997924

PL03, Algorithm

Back to the previous pagepage managementContents
List of posts to read before reading this article


Contents


Data structure

array

1d array

data_list = [1, 2, 3, 4, 5]




2d array

data_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]





Queue

FIFO Queue

import queue

data_queue = queue.Queue()
data_queue.put(0)
data_queue.put(1)
data_queue.put(2)
data_queue.put(3)
data_queue.put(4)
data_queue.get()
data_queue.get()
data_queue.get()
data_queue.get()
data_queue.get()
.qsize()
data_queue.qsize()


class fifo_queue:
    def __init__(self):
        self.list = list()
    
    def enqueue(self,x):
        self.list.append(x)
        print(x, 'was enqueued at', id(self.list[len(self.list)-1]))
        for i,j in enumerate(self.list):
            print(j, id(j)) if i != len(self.list)-1 else print('---'*10)
            
    def dequeue(self):
        dv = self.list[0]
        print('It was dequeued and present state of queue is as follow')
        del self.list[0]
        for i in self.list:
            print(i, id(i)) if i != self.list[-1] else print('---'*10)
        return dv
    
Q = fifo_queue()
Q.enqueue(0)
Q.enqueue(1)
Q.enqueue(2)
Q.dequeue()
Q.dequeue()
Q.dequeue()
OUTPUT
0 was enqueued at 10914464
0 10914464
------------------------------
1 was enqueued at 10914496
0 10914464
1 10914496
------------------------------
2 was enqueued at 10914528
0 10914464
1 10914496
2 10914528
------------------------------
It was dequeued and present state of queue is as follow
1 10914496
2 10914528
------------------------------
It was dequeued and present state of queue is as follow
2 10914528
------------------------------
It was dequeued and present state of queue is as follow




LIFO Queue

import queue

data_queue = queue.LifoQueue()
data_queue.put(0)
data_queue.put(1)
data_queue.put(2)
data_queue.put(3)
data_queue.put(4)




Priority Queue

.put((priority, data))

import queue

data_queue = queue.PriorityQueue()
data_queue.put((7,'7th data'))
data_queue.put((1,'1st data'))
data_queue.put((6,'6th data'))
data_queue.put((2,'2nd data'))
data_queue.put((4,'4th data'))





Stack

class stack():
    def __init__(self):
        self.stack_list = list()

    def push(self, data):
        return self.stack_list.append(data)

    def pop(self):
        data = self.stack_list[-1]
        del self.stack_list[-1]
        return data
s = stack()
s.push(1)
s.push(2)
s.pop()




Linked List

example




function 1




function 2




Doubly linked list





Recursion





Recursion

Factorial


Tower of Hanoi





Tree





Priority queue and heap





Sorting

fq0A8hx




Bubble Sort

giphy (1)





Selection Sort

AccomplishedCourageousGander-size_restricted





Insertion Sort

CornyThickGordonsetter-size_restricted





Heap Sort

Sorting_heapsort_anim





Merge Sort

merge-sort-gif-9





Quicksort

Sorting_quicksort_anim





Cocktail shaker sort

Sorting_shaker_sort_anim

















Table and Hash





Graph









Bellman-Ford algorithm





Dijkstra’s algorithm





A’ algorithm

Algorithm





List of posts followed by this article


Reference


OUTPUT