Algorithm learned with Python 18th: Sorting (stack and queue)

#Algorithms learned in Python

Introduction

Implement the basic algorithm in Python to deepen your understanding of the algorithm. The 18th bullet deals with stacks and queues. This time, it is a category of sorting, but it should be mentioned that stacks and queues are learned for comparison with the next sorting (heapsort), and are not sorting algorithms.

Stack

In the list where data is stored, extract from the end (the last one entered). Stack means to stack, as the name implies. The figure of the image in the cargo and the image in the list is shown below. image.png

Queue

In the list where data is stored, extract from the beginning (the one entered first). Queue means "column", and as the name suggests, the image is that what you put in comes out from the opposite side. As before, the image of the cargo and the image of the list are shown below. image.png In addition, as shown in the figure, in the queue, data storage and retrieval are named enqueue and dequeue.

Stack implementation

The stack code and its output are shown below.

code

stack.py


"""
2021/01/12
@Yuya Shimizu

stack(stack)
"""

List = []

List.append(3)         #on stack[3]Add
List.append(5)         #on stack[5]Add
List.append(2)         #on stack[2]Add

print(List)

temp = List.pop()     #Take out from stack
print(f"\n take out: {temp}\n")
print(List)

temp = List.pop()     #Take out from stack
print(f"\n take out: {temp}\n")
print(List)

List.append(4)         #on stack[4]Add
print(f"\n added: 4\n")
print(List)

temp = List.pop()     #Take out from stack
print(f"\n take out: {temp}\n")
print(List)
output
[3, 5, 2]

take out: 2

[3, 5]

take out: 5

[3]

add to: 4

[3, 4]

take out: 4

[3]

Queue implementation

The queue code and its output are shown below.

code

queue_program.py


"""
2021/01/12
@Yuya Shimizu

queue(queue)
"""
import queue

q = queue.Queue()

q.put(3)                #In the queue[3]Add
q.put(5)                #In the queue[5]Add
q.put(2)                #In the queue[2]Add

print(q.queue)

temp = q.get()      #Remove from queue
print(f"\n Decue: {temp}\n")
print(q.queue)

temp = q.get()      #Remove from queue
print(f"\n Decue: {temp}\n")
print(q.queue)

q.put(4)                #In the queue[4]Add
print(f"\n enqueue: 4\n")
print(q.queue)

temp = q.get()      #Remove from queue
print(f"\n Decue: {temp}\n")
print(q.queue)
output
deque([3, 5, 2])

Decue: 3

deque([5, 2])

Decue: 5

deque([2])

Enqueue: 4

deque([2, 4])

Decue: 2

deque([4])

Regarding queues, Python has a module called queue, and by using the Queue class, you can implement the put method, that is, the enqueue, and the get method, that is, the dequeue. Note that the queue module cannot be loaded with the name queue.py.

Impressions

This time I learned about stacks and cues. I didn't learn to sort directly, but I learned about a new queue for handling data. Also, in the stack, I handle pop () again, and I feel familiar with how to use pop (). I am looking forward to learning a better method than what I learned this time in the next heapsort.

References

Introduction to algorithms starting with Python: Standards and computational complexity learned with traditional algorithms Written by Toshikatsu Masui, Shoeisha

Recommended Posts

Algorithm learned with Python 18th: Sorting (stack and queue)
Algorithm learned with Python 19th: Sorting (heapsort)
Algorithm learned with Python 16th: Sorting (insertion sort)
Algorithm learned with Python 15th: Sorting (selection sort)
Algorithm learned with Python 17th: Sorting (bubble sort)
Algorithm learned with Python 10th: Binary search
Algorithm learned with Python 5th: Fibonacci sequence
Algorithm learned with Python 9th: Linear search
Algorithm learned with Python 7th: Year conversion
Algorithm learned with Python 8th: Evaluation of algorithm
Algorithm learned with Python 4th: Prime numbers
Algorithm learned with Python 6th: Leap year
Algorithm learned with Python 12th: Maze search
Algorithm learned with Python 11th: Tree structure
Algorithm learned with Python 13th: Tower of Hanoi
Algorithm learned with Python 14th: Tic-tac-toe (ox problem)
Stack and Queue in Python
Sorting algorithm and implementation in Python
Python #stack queue
Algorithm learned with Python 2nd: Vending machine
Algorithm learned with Python 3rd: Radix conversion
Use Python and word2vec (learned) with Azure Databricks
I tried it with SymPy Live, Wolfram Alpha and google with reference to "Algorithm learned with Python 4th: Prime numbers".
Programming with Python and Tkinter
Encryption and decryption with Python
Python and hardware-Using RS232C with Python-
Sorting image files with Python (2)
Sorting image files with Python (3)
Sorting image files with Python
[Python3] Dijkstra's algorithm with 14 lines
python with pyenv and venv
Works with Python and R
Try sorting your own objects with priority queue in Python
Solve the spiral book (algorithm and data structure) with python!
This time I learned python III and IV with Prorate
Communicate with FX-5204PS with Python and PyUSB
Robot running with Arduino and python
Install Python 2.7.9 and Python 3.4.x with pip.
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
[Python] font family and font with matplotlib
Scraping with Node, Ruby and Python
Scraping with Python, Selenium and Chromedriver
[Python] Object-oriented programming learned with Pokemon
JSON encoding and decoding with python
Hadoop introduction and MapReduce with Python
[GUI with Python] PyQt5-Drag and drop-
Perceptron learning experiment learned with Python
Python data structures learned with chemoinformatics
Efficient net pick-up learned with Python
Reading and writing NetCDF with Python
FX automatic trading system made with python and genetic algorithm Part 1
1. Statistics learned with Python 1-1. Basic statistics (Pandas)
I played with PyQt5 and Python3
Reading and writing CSV with Python
Implementation of Dijkstra's algorithm with python
Show stack trace with python exception
Multiple integrals with Python and Sympy
Coexistence of Python2 and 3 with CircleCI (1.0)
Easy modeling with Blender and Python
Sugoroku game and addition game with python