Alignment algorithm by insertion method in Python

study

The book I was reading came to the algorithm chapter. Practice of Insert Sort is. Like this Sort the contents of the array in ABC order Sort.

insertion_sort.py


# coding: UTF-8
#Japanese cannot be included in the code without ↑
List = ['Fred', 'Alex', 'Diana', 'Byron', 'Carol' ]

#Insertion method
N = 2 #The insertion method begins with touching the second element

while len(List) >= N: #As long as the value of N does not exceed the length of the List
    pip = List[N-1] #Pivot the Nth element, move the contents to a temporary place,
    List[N-1] = 'BLANK' #Make it a gap (it is not necessary to specify the gap)
    
    sukima = N-1 #Position of the first gap
    while sukima != 0: #As long as "the name is above the gap (= the current gap is the second and subsequent elements)"
        if List[sukima-1] > pip: #If "just above the gap is larger than the pivot"
            List[sukima] = List[sukima-1] #Drop it in the gap
            sukima = sukima-1 #The top is a new gap.
        else: break

    List[sukima] = pip #Return the pivot to the gap.
    N = N + 1 #Increment and repeat processing

#output
for x in List:
    print x 

On the way, compare the size of the pivot and the string of the element. I'm not sure what "character string size" means here. But min (List) returns Arex and max (List) returns Fred, so I presume that the later in the ABC order, the larger the character string.

Execution result

Result It's fun if it works.

References

Introduction to Computer Science p218-p222

Recommended Posts

Alignment algorithm by insertion method in Python
Genetic algorithm in python
Simplex method (simplex method) in Python
Algorithm in Python (Bellman-Ford)
Private method in python
Algorithm in Python (Dijkstra's algorithm)
Implemented in Python PRML Chapter 4 Classification by Perceptron Algorithm
Algorithm in Python (primality test)
Reproduce Euclidean algorithm in Python
Algorithm in Python (binary search)
Implement method chain in Python
Implement Dijkstra's Algorithm in python
Suppressing method overrides in Python
Sort by date in python
Algorithm in Python (breadth-first search, bfs)
Sorting algorithm and implementation in Python
Write A * (A-star) algorithm in Python
Develop an investment algorithm in Python 2
Algorithm in Python (depth-first search, dfs)
Implemented label propagation method in Python
Implementing a simple algorithm in Python 2
Algorithm (segment tree) in Python (practice)
Python algorithm
Method to build Python environment in Xcode 6
Electron Microscopy Simulation in Python: Multislice Method (1)
Electron Microscopy Simulation in Python: Multislice Method (2)
Read the file line by line in Python
Automate jobs by manipulating files in Python
Read the file line by line in Python
Common mock by moto in Python Unittest
Scene recognition by GIST features in Python
Quadtree in Python --2
Python in optimization
CURL in python
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Learn the design pattern "Template Method" in Python
Algorithm learned with Python 16th: Sorting (insertion sort)
Epoch in Python
Discord in Python
To dynamically replace the next method in python
Learn the design pattern "Factory Method" in Python
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
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Reflection in Python
Constant in python
nCr in Python.