Implemented the algorithm of "Algorithm Picture Book" in Python3 (selection sort)

About this article

In this article, I would like to introduce an implementation example in Python 3 about the algorithm that I learned by reading "Algorithm Picture Book". The algorithm this time is selection sort. The writer is an amateur. I would appreciate it if you could tell me various things.

I'm not familiar with Python2, but I only know that I'm using Python3 (Is it Python3.6.0?). Therefore, the title of the article is Python3.

About selection sort

I will only briefly explain the problem setting and approach.

problem

Returns the columns sorted by the smallest number for a given number of columns. Example:  4, 3, 1, 2 → 1, 2, 3, 4

approach

Determine the values in order from the beginning. Find the minimum value from the undetermined number and bring it to the front (first side) by swapping. See "Algorithm Picture Book" for details.

Example: Notation of fixed numbers enclosed in []  4, 3, 1, 2 → [1], 3, 4, 2 → [1], [2], 4, 3 → [1], [2], [3], 4

Implementation code and execution result

The implemented code is shown below. The list assigned to the variable data first is the column of the number to be processed. Also, I implemented it without using min in the list.

code

selection_sort.py


data = [4, 3, 1, 2]
print("input    :" + str(data))

data_len = len(data)

for k in range(0, data_len - 1):
    min_index = k
    min_data = data[k]
    for i in range(k + 1, data_len):
        if data[i] < min_data:
            min_index = i
            min_data = data[i]
        else:
            pass
    data[min_index] = data[k]
    data[k] = min_data

print("output   :" + str(data))

Execution result

python


$ python selection_sort.py 
input    :[4, 3, 1, 2]
output   :[1, 2, 3, 4]

At the end

I posted it for the first time as a practice to post the code I wrote. If you have any questions, please point out and ask questions. Especially if there are any improvements in how to write the code, I think it will be useful for studying.

Recommended Posts

Implemented the algorithm of "Algorithm Picture Book" in Python3 (selection sort)
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Bubble Sort)
[Python] Sort the list of pathlib.Path in natural sort
Sort in Python. Next, let's think about the algorithm.
Picture book data structure algorithm Python
Check the behavior of destructor in Python
Ant book in python: Sec.2-5 Dijkstra's algorithm
The result of installing python in Anaconda
The basics of running NoxPlayer in Python
In search of the fastest FizzBuzz in Python
What kind of book is the best-selling "Python Crash Course" in the world?
Output the number of CPU cores in Python
Get the caller of a function in Python
Match the distribution of each group in Python
View the result of geometry processing in Python
Make a copy of the list in Python
Algorithm learned with Python 15th: Sorting (selection sort)
Find the solution of the nth-order equation in python
The story of reading HSPICE data in Python
[Note] About the role of underscore "_" in Python
About the behavior of Model.get_or_create () of peewee in Python
Solving the equation of motion in Python (odeint)
Output in the form of a python array
I implemented the inverse gamma function in python
I tried to implement selection sort in python
[Fundamental Information Technology Engineer Examination] I wrote the algorithm of Euclidean algorithm in Python.
Bubble sort in Python
Implemented SimRank in Python
Genetic algorithm in python
Algorithm in Python (Bellman-Ford)
Custom sort in Python3
Implemented Shiritori in Python
Algorithm in Python (Dijkstra's algorithm)
Experience the good calculation efficiency of vectorization in Python
Basic information Write the 2018 fall algorithm problem in Python
[python] Get the list of classes defined in the module
Ruby, Python code fragment execution of selection in Emacs
The story of FileNotFound in Python open () mode ='w'
Implement the solution of Riccati algebraic equations in Python
Get the size (number of elements) of UnionFind in Python
Not being aware of the contents of the data in python
Reproduce the execution example of Chapter 4 of Hajipata in Python
Let's use the open data of "Mamebus" in Python
[Python] Outputs all combinations of elements in the list
Get the URL of the HTTP redirect destination in Python
A reminder about the implementation of recommendations in Python
Reproduce the execution example of Chapter 5 of Hajipata in Python
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
Check the asymptotic nature of the probability distribution in Python
Sort tuple list in Python by specifying the ascending / descending order of multiple keys
Try scraping the data of COVID-19 in Tokyo with Python
Find out the apparent width of a string in python
Towards the retirement of Python2
Download the file in Python
Measure the execution result of the program in C ++, Java, Python.
Check the operation of Python for .NET in each environment
[Memo] The mystery of cumulative assignment statements in Python functions
Find the difference in Python
Algorithm in Python (primality test)
Naturally sort Path in Python
The result of Java engineers learning machine learning in Python www