I tried to implement selection sort in python

I've been studying python and algorithms, so I implemented selection sort.

  1. Create a random list Example: [1,3,0,4,2]

  2. Find the smallest value in the operation target list Example: [1,3,0,4,2] → 0 is the minimum

  3. Replace the leftmost with 0, and 0 completes the operation.

[1,3,0,4,2]→[0,3,1,4,2]

-Next is the second from the left, then the third ... and so on, and repeat steps 2 and 3-

[1,3,0,4,2] [0,3,1,4,2] [0,1,3,4,2] [0,1,2,4,3] [0,1,2,3,4]

It seems that the process of ... is called selection sort.

select_sort.py



import random

#Enter the number of values to prepare
numbers = 10

#Create a list to store checked values
pop_list = []

#Random number creation
num_list = list(range(numbers))
random.shuffle(num_list)

def _select_min(num_list, pos):

    _min = None
    _left = num_list[pos]
    _pos = 0

    for i in range(pos, len(num_list)):
        if _min == None or _min > num_list[i]:
            _min = num_list[i]
            _pos = i
    
    num_list[pos] = _min
    num_list[_pos] = _left

    return(num_list)

#Main processing
print("START : ",num_list)
for i in range(len(num_list)):
    num_list = _select_min(num_list, i)
print("GOAL  : ",num_list)

Recommended Posts

I tried to implement selection sort in python
I tried to implement PLSA in Python
I tried to implement permutation in Python
I tried to implement PLSA in Python 2
I tried to implement ADALINE in Python
I tried to implement PPO in Python
I tried to implement TOPIC MODEL in Python
I tried to implement Dragon Quest poker in Python
I tried to implement merge sort in Python with as few lines as possible
I tried to implement a one-dimensional cellular automaton in Python
I tried to implement the mail sending function in Python
I tried to implement blackjack of card game in Python
I tried to implement PCANet
I tried to implement StarGAN (1)
I tried to implement Bayesian linear regression by Gibbs sampling in python
I tried to implement a card game of playing cards in Python
I tried to graph the packages installed in Python
I want to easily implement a timeout in python
I tried to implement Minesweeper on terminal with python
I tried to implement an artificial perceptron with python
I tried to summarize how to use pandas in python
I tried to implement Deep VQE
I tried to touch Python (installation)
I tried to implement hierarchical clustering
I tried to implement Realness GAN
I tried to implement what seems to be a Windows snipping tool in Python
I tried Line notification in Python
I want to do something like sort uniq in Python
I tried "How to get a method decorated in Python"
I tried to implement Harry Potter sort hat with CNN
I tried to make a stopwatch using tkinter in python
I tried to summarize Python exception handling
I tried to implement Autoencoder with TensorFlow
I tried using Bayesian Optimization in Python
I wrote the selection sort in C
I wanted to solve ABC159 in Python
I tried to implement CVAE with PyTorch
[Python] I tried to calculate TF-IDF steadily
I tried to touch Python (basic syntax)
[Python] I tried to implement stable sorting, so make a note
Implement Enigma in python
I tried to program bubble sort by language
I tried to implement reading Dataset with PyTorch
I want to do Dunnett's test in Python
Try to implement Oni Maitsuji Miserable in python
[Python] I tried to summarize the set type (set) in an easy-to-understand manner.
How to implement Discord Slash Command in Python
Bubble sort in Python
I was able to recurse in Python: lambda
I want to create a window in Python
I tried playing a typing game in Python
How to implement shared memory in Python (mmap.mmap)
I tried Python> autopep8
I tried to integrate with Keras in TFv1.1
I tried simulating the "birthday paradox" in Python
I tried the least squares method in Python
Implement recommendations in Python
I wrote "Introduction to Effect Verification" in Python
Implement XENO in python
I tried to get CloudWatch data with Python
I tried to output LLVM IR with Python