Easy Tutorial
❮ Python Os Getcwd Python Func Number Uniform ❯

Python Selection Sort

Python3 Examples

Selection sort is a simple and intuitive sorting algorithm. Its working principle is as follows: first, find the smallest (or largest) element in the unsorted sequence and place it at the beginning of the sorted sequence. Then, continue to find the smallest (or largest) element from the remaining unsorted elements and place it at the end of the sorted sequence. This process is repeated until all elements are sorted.

Example

import sys 
A = [64, 25, 12, 22, 11] 

for i in range(len(A)): 

    min_idx = i 
    for j in range(i+1, len(A)): 
        if A[min_idx] > A[j]: 
            min_idx = j 

    A[i], A[min_idx] = A[min_idx], A[i] 

print ("Sorted array:") 
for i in range(len(A)): 
    print("%d" %A[i]),

Executing the above code produces the following result:

Sorted array:
11
12
22
25
64

Python3 Examples

❮ Python Os Getcwd Python Func Number Uniform ❯