A memo about writing merge sort in Python

I wrote a note for myself about merge sort.

What is merge sort?

--A high-speed sorting algorithm based on the divide-and-conquer method that can handle large data sizes. --Computational complexity is O (nlogn) --Because there are logn layers and the amount of calculation for each layer is O (n). --Stable sorting that does not change the order of the same values even if sorted --Requires memory to temporarily store data

code

marge_sort.py


def merge(A, left, mid, right):
    
    L = []
    for i in range(mid - left):
        L.append(A[left + i])
    L.append(1000)    #Make sure the number is larger than the number to be sorted
    
    R = []
    for i in range(right - mid):
        R.append(A[mid + i])
    R.append(1000)    #Make sure the number is larger than the number to be sorted
    
    i = j = 0
    for k in range(left, right):
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1
    
def merge_sort(A, left, right):
    if left+1 < right:
        mid = (left + right) // 2
        merge_sort(A, left, mid)
        merge_sort(A, mid, right)
        merge(A, left, mid, right)
    return A

print(merge_sort([3, 1, 10, 2.5, 11, 3, 21, 4, -1], 0, 9))
# [-1, 1, 2.5, 3, 3, 4, 10, 11, 21]

This time you can sort numbers less than 1000 in ascending order 1000 is set appropriately, so if you increase it, you can sort even larger numbers. I didn't make the total number of calls well ...

reference

Merge sort in C language Sort algorithm and implementation in Python [Unity] I tried to visualize 12 sort algorithms

Recommended Posts

A memo about writing merge sort in Python
When writing a program in Python
A memo of writing a basic function in Python using recursion
A memo that I wrote a quicksort in Python
Bubble sort in Python
[Python] Memo about functions
[CpawCTF] Q14. [PPC] Try writing Sort! In Python
[Memo] I tried a pivot table in Python
[Memo] Python3 list sort
[Python] Memo about errors
Data analysis in Python: A note about line_profiler
Think about building a Python 3 environment in a Mac environment
Custom sort in Python3
Sort list elements in a specified order in Python
About __all__ in python
[Python] Manipulating elements in a list (array) [Sort]
Sort in Python. Next, let's think about the algorithm.
Write about building a Python environment for writing Qiita Qiita
Merge sort implementation / complexity analysis and experiment in Python
A memorandum when writing experimental code ~ Logging in python
A memo about building a Django (Python) application with Docker
A reminder about the implementation of recommendations in Python
Take a screenshot in Python
Create a function in Python
Naturally sort Path in Python
Think about architecture in python
python in mongodb in descending sort
A memorandum about correlation [Python]
Make a bookmarklet in Python
A memorandum about Python mock
Draw a heart in Python
About "for _ in range ():" in python
Sort by date in python
About Python sort () and reverse ()
A note about [python] __debug__
A story about how to specify a relative path in python.
A memo when creating a directed graph using Graphviz in Python
How to develop in a virtual environment of Python [Memo]
A story about trying to implement a private variable in Python.
Maybe in a python (original title: Maybe in Python)
Write a binary search in Python
[python] Manage functions in a list
Hit a command in Python (Windows)
What's new in python3.9 Merge dictionaries
Create a DI Container in Python
Python: A Note About Classes 1 "Abstract"
Web application development memo in python
Draw a scatterplot matrix in python
A note about get_scorer in sklearn
ABC166 in Python A ~ C problem
About dtypes in Python and Cython
Write A * (A-star) algorithm in Python
Sort large text files in Python
Solve ABC036 A ~ C in Python
Get, post communication memo in Python
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python
I learned about processes in Python
[Python] Creating a scraping tool Memo
Implementing a simple algorithm in Python 2