Implemented Stooge sort in Python3 (Bubble sort & Quicksort)

[There seems to be a sort called Stooge sort. ](Https://ja.wikipedia.org/wiki/%E3%82%B9%E3%83%88%E3%82%A5%E3%83%BC%E3%82%B8%E3%82%BD % E3% 83% BC% E3% 83% 88) This sort algorithm seems to be a very __inefficient __sort algorithm, which is less efficient than bubble sort.

In this article, I'll implement Stoogesort in Python3.


#Sort the list destructively. The algorithm is "Stooge sort"
def stooge_sort(ls):
    stack = [(0, len(ls) - 1)]
    
    while stack:
        i, j = stack.pop()
        if ls[i] > ls[j]: ls[i], ls[j] = ls[j], ls[i]
        
        if j - i + 1 >= 3:
            t = (j - i + 1) // 3
            stack.extend(((i, j - t), (i + t, j), (i, j - t)))

if __name__ == '__main__':
    import random

    ls = list(range(30))
    random.shuffle(ls)

    print(ls)
    stooge_sort(ls)
    print(ls)

# [20, 4, 13, 28, 12, 0, 17, 19, 22, 21, 5, 23, 3, 27, 14, 2, 29, 11, 24, 7, 15, 9, 25, 6, 26, 18, 8, 1, 10, 16]
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]

It seems that you can sort properly. The idea (?) Is that it is implemented repeatedly instead of recursion ... Is it meaningful to implement it efficiently for inefficient algorithms? (´ ・ ω ・ `)

Next is efficiency. [Inefficient "bubble sort"](https://ja.wikipedia.org/wiki/%E3%83%90%E3%83%96%E3%83%AB%E3%82 % BD% E3% 83% BC% E3% 83% 88) and Efficient "Quicksort" I would like to implement% E3% 83% 83% E3% 82% AF% E3% 82% BD% E3% 83% BC% E3% 83% 88) and measure the time to sort a list with 1000 elements. I think.

#Sort the list destructively. The algorithm is "Stooge sort"
def stooge_sort(ls):
    stack = [(0, len(ls) - 1)]
    
    while stack:
        i, j = stack.pop()
        if ls[i] > ls[j]: ls[i], ls[j] = ls[j], ls[i]
        
        if j - i + 1 >= 3:
            t = (j - i + 1) // 3
            stack.extend(((i, j - t), (i + t, j), (i, j - t)))


#Sort the list destructively. The algorithm is "bubble sort"
def bubble_sort(ls):
    t = len(ls)

    for i in range(t - 1):
        for j in range(i + 1, t):
            if ls[i] > ls[j]: ls[i], ls[j] = ls[j], ls[i]


#Sort the list destructively. The algorithm is "quicksort"
def quick_sort(ls):
    stack = [(0, len(ls) - 1)]

    while stack:
        left, right = stack.pop()
        if left >= right: continue

        pivot = ls[left + (right - left) // 2]
        i, j = left, right
        while True:
            while ls[i] < pivot: i += 1
            while ls[j] > pivot: j -= 1

            if i >= j: break
            
            ls[i], ls[j] = ls[j], ls[i]
            i, j = i + 1, j - 1

        stack.extend(((j + 1, right), (left, i - 1)))

if __name__ == '__main__':
    import random, copy, time

    ls = list(range(1000))
    random.shuffle(ls)
    
    bubble_ls = copy.deepcopy(ls)
    bubble_start = time.time()
    bubble_sort(bubble_ls)
    bubble_time = time.time() - bubble_start

    quick_ls = copy.deepcopy(ls)
    quick_start = time.time()
    quick_sort(quick_ls)
    quick_time = time.time() - quick_start

    stooge_ls = copy.deepcopy(ls)
    stooge_start = time.time()
    stooge_sort(stooge_ls)
    stooge_time = time.time() - stooge_start

    print("bubble : {}".format(bubble_time))
    print("quick  : {}".format(quick_time))
    print("stooge : {}".format(stooge_time))

    # bubble : 0.0938718318939209
    # quick  : 0.0
    # stooge : 33.47836709022522

Slow ((((´ ・ ω ・ `))))

Recommended Posts

Implemented Stooge sort in Python3 (Bubble sort & Quicksort)
Bubble sort in Python
quicksort in python
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Bubble Sort)
Implemented SimRank in Python
Custom sort in Python3
Implemented Shiritori in Python
Quicksort an array in Python 3
Naturally sort Path in Python
python in mongodb in descending sort
Python beginners organize bubble sort
Sudoku solver implemented in Python 3
Implementation of quicksort in Python
6 Ball puzzle implemented in python
Sort by date in python
Implemented the algorithm of "Algorithm Picture Book" in Python3 (selection sort)
Implemented image segmentation in python (Union-Find)
Implemented label propagation method in Python
Sort large text files in Python
Implemented Perceptron learning rules in Python
Implemented in 1 minute! LINE Notify in Python
[Python] Sort
Python # sort
Bubble sort
Bubble sort
When specifying multiple keys in python sort
Implemented in Python PRML Chapter 7 Nonlinear SVM
I implemented Cousera's logistic regression in Python
Implemented in Python PRML Chapter 5 Neural Networks
Implemented in Python PRML Chapter 1 Bayesian Inference
Implemented in Python PRML Chapter 3 Bayesian Linear Regression
Quadtree in Python --2
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
I implemented Robinson's Bayesian Spam Filter in python
[Python] Sort the list of pathlib.Path in natural sort
A memo that I wrote a quicksort in Python
Meta-analysis in Python
Unittest in python
Implemented memoization recursion and exploration in Python and Go
Discord in Python
[CpawCTF] Q14. [PPC] Try writing Sort! In Python
DCI in Python
Implemented in Python PRML Chapter 1 Polynomial Curve Fitting
nCr in python
[Neta] Thread-safe Sleep Sort function in Python (threading)
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Algorithm learned with Python 17th: Sorting (bubble sort)
Lifegame in Python.
Sqlite in python
StepAIC in Python
I implemented the inverse gamma function in python
N-gram in python
LINE-Bot [0] in Python
Csv in python