[GO] Basic sorting in Python

I wrote a basic sort for studying Python and algorithms. I will continue to add more if I feel like it.

Selection sort

def sSort(a):
    for i in range(len(a)-1):
        mi = a[i:].index(min(a[i:]))
        a[i], a[i+mi] = a[i+mi], a[i]
    
    return a

Bubble sort

def bSort(a):
    for i in range(len(a)):
        for j in range(len(a)-1, i, -1):
            if a[j] < a[j-1]:
                a[j], a[j-1] = a[j-1], a[j]
            
    return a

Insertion sort

def iSort(a):
    for i in range(1, len(a)):
        for j in range(i, 0, -1):
            if a[j] >= a[j-1]:
                break
            else:
                a[j], a[j-1] = a[j-1], a[j]
    
    return a

Quick sort

def qSort(a):
    if len(a) in (0, 1):
        return a
    
    p = a[-1]
    l = [x for x in a[:-1] if x <= p]
    r = [x for x in a[:-1] if x >  p]

    return qSort(l) + [p] + qSort(r)

Merge sort

def merge(l, r):
    n = len(l + r) #Array size after merging
    s = max(l + r) + 1 #Guard

    l.append(s)
    r.append(s)

    a = []
    while len(a) < n:
        a.append(l.pop(0)) if l[0] <= r[0] else a.append(r.pop(0))
    
    return a

def mSort(a):
    if len(a) == 1:
        return a

    mid = len(a) // 2
    l = mSort(a[:mid])
    r = mSort(a[mid:])
    
    return merge(l, r)

Recommended Posts

Basic sorting in Python
Refactoring Learned in Python (Basic)
Techniques for sorting in Python
Sorting algorithm and implementation in Python
Scraping with Selenium in Python (Basic)
[Python] Basic knowledge used in AtCoder
Implementation of original sorting in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
Basic Python writing
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
Python3 basic grammar
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
RF Python Basic_02
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Basic Linear Algebra Learned in Python (Part 1)
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python