Selection Sort : How to implement it in Python ?

In this tutorial, we will learn the procedure of sorting an unsorted list using the pre-defined algorithm of Selection Sort in Python.

Also, have a look at other sorting methods :


Selection Sort in Python

The basic logic of Selection Sort is to repeatedly select the smallest element in the unsorted part of the array and then swap it with the first element of the unsorted part of the list.

ADVANTAGES Of Selection Sort :

  • Too simple to understand: Selection sort is the very first building method to teach sorting techniques in coding due to it being easily readable and easy to understand.
  • The sorting of elements doesn’t depend on the initial arrangement of the elements.

DISADVANTAGES Of Selection Sort :

  •  Inefficient when the lists are large.
  •  Its performance is worse than the Insertion sort algorithm.


Selection sort in the worst case scenario would make n*n comparisons (due to 2 combined loops); making the time required to be O(n2), where n is the number of elements in the array.


Selection Sort takes constant space irrespective of the number of elements in the array taking the space required to be O(1).

Implementation of selection sort in Python

data_list=list(map(int , input().split()))
for i in range(0, len(data_list)):
    for j in range(i+1,len(data_list)):


2 34 43 1 32 657 76 32 21


[1, 2, 21, 32, 32, 34, 43, 76, 657]

Explanation :

The code involves two nested loops. The outside loop tracks the current position that the code wants to swap the smallest value with. The inside loop starts at the current location and scans the rest of the list in search of the smallest value. When it finds the smallest value, swapping of elements takes place.


That’s it for now!

Hope you have understood the algorithm.

Drop your queries regarding this tutorial in the comment section below.

Also, have a look at other posts :

Leave a Reply

Your email address will not be published. Required fields are marked *