PL03, Algorithm
Back to the previous page |page management| Contents
List of posts to read before reading this article
Contents
- Data structure
- Recursion
- Recursion
- Tree
- Priority queue and heap
- Sorting
- Search
- Table and Hash
- Graph
- Algorithm
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
Bubble Sort
Selection Sort
Insertion Sort
Heap Sort
Merge Sort
Quicksort
Cocktail shaker sort
Search
Linear search
Binary search
Ternary search
Table and Hash
Graph
Breadth-First search
Depth-First search
Bellman-Ford algorithm
Dijkstra’s algorithm
A’ algorithm
Algorithm
List of posts followed by this article
- arithmetic analysis
- backtracking
- blockchain
- boolean algebra
- ciphers
- compression
- conversions
- digital image processing
- divide and conquer
- dynamic programming
- file transfer
- linear algebra
- machine learning
- maths
- matrix
- networking-flow
- neural-network
- other
- strings
- traversals
Reference
- dojang
- fun coding
- Implementation with python2 on web
- Implementation with python3 on web
- visualgo
- TheAlgorithms
- Visualizing Algorithms through animation
- post3