Implemented the algorithm of "Algorithm Picture Book" in Python3 (Bubble 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 bubble 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 bubble sort

The problem settings are as follows. The explanation of the algorithm is omitted. Please refer to "Algorithm Picture Book".

problem

Returns the columns sorted by the smallest number for a given number of columns. Example:  4, 3, 1, 2 → 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.

code

bubble_sort.py


data = [4, 3, 1, 2]

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

data_len = len(data)

for k in range(0, data_len - 1):
    i = data_len - 1
    while(i - 1 >= k):
        if data[i - 1] > data[i]:
            temp_data = data[i - 1]
            data[i - 1] = data[i]
            data[i] = temp_data
        else:
            pass
        i -= 1

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

Execution result

python


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

At the end

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 (Bubble Sort)
Implemented the algorithm of "Algorithm Picture Book" in Python3 (selection sort)
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Heapsort)
Implemented Stooge sort in Python3 (Bubble sort & Quicksort)
Bubble sort in Python
[Python] Sort the list of pathlib.Path in natural sort
Sort in Python. Next, let's think about the algorithm.
Implemented bubble sort in Java (BubbleSort)
Picture book data structure algorithm 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
Make a copy of the list in Python
Find the divisor of the value entered in python
Algorithm learned with Python 17th: Sorting (bubble 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
[Fundamental Information Technology Engineer Examination] I wrote the algorithm of Euclidean algorithm in Python.
Implemented SimRank in Python
Genetic algorithm in python
the zen of 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
How to get the number of digits in Python
Implemented in Python PRML Chapter 4 Classification by Perceptron Algorithm
[python] Get the list of classes defined in the module
The story of FileNotFound in Python open () mode ='w'
Learn the design pattern "Chain of Responsibility" in Python
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
Find out the apparent width of a string in python
I tried the accuracy of three Stirling's approximations in python
Towards the retirement of Python2
Download the file in 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