Python Program for Selection Sort
Saturday, March 16, 2019

Python Program for Selection Sort


Write an Article

  • Python Program for Bubble Sort
  • Python List
  • How to print without newline in Python?
  • Program to print ASCII Value of a character
  • Stormer Numbers
  • Python Program for QuickSort
  • Python Program for Merge Sort
  • Linear Regression Using Tensorflow
  • Python | Catching the ball game
  • Generating Word Cloud in Python
  • Minimum Increment operations to make Array unique
  • Python Program to find sum of array
  • Python Program to Reverse a linked list
  • Python program to split and join a string
  • Python | Program to print duplicates from a list of integers
  • Python Program for Sieve of Eratosthenes
  • Python implementation of automatic Tic Tac Toe game using random number
  • Hangman Game in Python
  • Python program to find N largest elements from a list
  • Python | Find current weather of any city using openweathermap API
  • Python | Reading an excel file using openpyxl module
  • Program to print all the numbers divisible by 3 and 5 for a given number
  • Python program to convert time from 12 hour to 24 hour format
  • Rename multiple files using Python
  • Python Program for Tower of Hanoi
  • Python program to add two numbers
  • Binary Search (bisect) in Python
  • Python Program to find largest element in an array

Python Program for Selection Sort

The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array.

1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.

In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray.






# Python program for implementation of Selection
# Sort
import sys
A = [64, 25, 12, 22, 11]
# Traverse through all array elements
for i in range(len(A)):
    # Find the minimum element in remaining
    # unsorted array
    min_idx = i
    for j in range(i+1, len(A)):
        if A[min_idx] > A[j]:
            min_idx = j
    # Swap the found minimum element with
    # the first element       
    A[i], A[min_idx] = A[min_idx], A[i]
# Driver code to test above
print ("Sorted array")
for i in range(len(A)):
    print("%d" %A[i]),


Please refer complete article on Selection Sort for more details!

My Personal Notes

Recommended Posts:

  • Python program to convert time from 12 hour to 24 hour format
  • Python Program for Bubble Sort
  • Python Program for Insertion Sort
  • Python Program for Heap Sort
  • Python Program for Counting Sort
  • Python Program for Radix Sort
  • Python Program for n-th Fibonacci number
  • Python Program for Counting Sort
  • Python Program for ShellSort
  • Python Program for Longest Common Subsequence
  • Python Program for Binary Search (Recursive and Iterative)
  • Python Program for 0-1 Knapsack Problem
  • Python Program for Min Cost Path
  • Python program for Longest Increasing Subsequence
  • Python Program for GCD of more than two (or array) numbers









Writing code in comment? Please use , generate link and share the link here.

Most popular in Python Programs
  • Python Program for Fibonacci numbers
  • Python program to Swap Keys and Values in Dictionary
  • Python program to count positive and negative numbers in a list
  • Python Program for n-th Fibonacci number
  • Python Program to check Armstrong Number

More related articles in Python Programs
  • Python | Program to convert a tuple to a string
  • Python Program for Program to find area of a circle
  • Python program to create a list of tuples from given list having number and its cube in each tuple
  • Python Program for n\’th multiple of a number in Fibonacci Series
  • Program to replace a word with asterisks in a sentence

Most visited in Python Programs
  • Check if the given push and pop sequences of Stack is valid or not
  • Program to print the pattern ‘D’
  • Python | Program to accept the strings which contains all vowels
  • Python program to count number of vowels using sets in given string
  • Python program to check whether a number is Prime or not
  • Python | Program to check if a string contains any special character
  • Python program to print all Prime numbers in an Interval
  • Python Program for How to check if a given number is Fibonacci number?
  • Python program to find the most occurring character and its count
  • Python Program for simple interest
  • Python program for removing i-th character from a string
  • Python Program for Find largest prime factor of a number
  • Python program to find common elements in three lists using sets
  • Program to create grade calculator in Python
  • Python Program for Insertion Sort

Python Central Logo

Selection Sort: A Quick Tutorial and Implementation Guide

This article is part 8 of 14 in the series

Python Data Structures Tutorial

Last Updated: Wednesday 20th September 2017


To learn about Selection Sort, you must know:

  1. Python 3
  2. Python data structures – Lists

What is Selection Sort?

We are in the third tutorial of the sorting series. The previous tutorial talks about Bubble Sort and Insertion Sort . If you haven’t read that, please do as we will be building off of those concepts. Like all sorting algorithms, we consider a list to be sorted only if it is in the ascending order. Descending order is considered the worst unsorted case.

Similar to Insertion Sort, we begin by considering the first element to be sorted and the rest to be unsorted. As the algorithm proceeds, the sorted portion of the list will grow and the unsorted portion will continue to shrink.

Selection Sort is about picking/selecting the smallest element from the list and placing it in the sorted portion of the list. Initially, the first element is considered the minimum and compared with other elements. During these comparisons, if a smaller element is found then that is considered the new minimum. After completion of one full round, the smallest element found is swapped with the first element. This process continues till all the elements are sorted. 

Let us see this using an example, alist = [5,9,1,2,7,0]

5 is considered sorted and the elements 9,1,2,7,0 are considered unsorted.

Round 1:

5 is the minimum5<9: nothing happens5>1: 1 is new minimum1<2: nothing happens1<7: nothing happens1>0: 0 is the new minimumSwap 0 and 5alist =  [0,9,1,2,7,5]

Round 2:

9 is the minimum9>1: 1 is the new minimum1<2: nothing happens1<7: nothing happens1<5: nothing happensSwap 1 and 9alist = [0,1,9,2,7,5]

Round 3:

9 is considered minimum9>2: 2 is the new minimum2<7: nothing happens2<5: nothing happensSwap 2 and 9alist = [0,1,2,9,7,5]

Round 4:

9 is considered minimum9>7: 7 is the new minimum7>5: 5 is the new minimumSwap 9 and 5alist = [,7,9]

Round 5:

7 is considered minimum7<9: nothing happensalist = [,7,9]

Note: even though after round 4 we can see the list is sorted, there is no way for the algorithm to know this. Hence only after completely traversing the entire list, the algorithm stops. 

See this animation for better understanding.

How to implement Selection Sort?

Now that you have a fair understanding of what Selection Sort is, let us take a look at the algorithm and its code.


  1. Consider the first element to be sorted and the rest to be unsorted
  2. Assume the first element to be the smallest element.
  3. Check if the first element is smaller than each of the other elements:
    1. If yes, do nothing
    2. If no, choose the other smaller element as minimum and repeat step 3
  4. After completion of one iteration through the list, swap the smallest element with the first element of the list.
  5. Now consider the second element in the list to be the smallest and so on till all the elements in the list are covered.

NOTE: Once an element is added to the sorted portion of the list, it must never be touched and or compared.


def selectionSort(alist):
   for i in range(len(alist)):
      # Find the minimum element in remaining
       minPosition = i
       for j in range(i+1, len(alist)):
           if alist[minPosition] > alist[j]:
               minPosition = j
       # Swap the found minimum element with minPosition       
       temp = alist[i]
       alist[i] = alist[minPosition]
       alist[minPosition] = temp
   return alist


Selection Sort works best with a small number of elements. The worst case runtime complexity of Insertion Sort is o(n2) similar to that of Insertion and Bubble Sort. That’s it for this tutorial. Happy Pythoning!

Previous Article

Next Article

  1. Home
  2. Python How To’s
  3. Python Tutorials
  4. Python Data Structures Tutorial

Related Articles

Signup for new content

Latest Articles

  • How To Reverse a Singly Linked List
  • Singly Linked List: How To Find and Remove a Node
  • Singly Linked List: How To Insert and Print Node
  • List in Python: How To Implement in Place Reversal
  • Quick Sort: A Tutorial and Implementation Guide


Python is a beautiful language.

Sign in Get started

Selection Sort Algorithm in Python

Go to the profile of Avinash Nethala
Avinash Nethala

Hello everyone, welcome back to ! In continuation of the algorithm series here is one of the sorting algorithms. Here in this post, I will discuss on Selection Sort algorithm and how to implement Selection Sort in Python.

In Selection Sort algorithm, the smallest element is searched and placed at the starting of the list by swapping the smallest element found with the first element of the list. So here, the list has 2 parts, one is sorted and the other is unsorted. Initially, the whole list is unsorted, but when each swapping occurs the leftmost part of the list becomes sorted and continues until the whole list is sorted.

Selection Sort Algorithm —

Time Complexity Of Selection Sort

Best CaseO — (n2)

Average CaseO — (n2)

Worst CaseO — (n2)


Given a list L of n elements with values or records L0, L1, …, Ln-1, bubble sort is applied to sort the list L.

  1. Find the smallest element in the array
  2. Swap the position(index) of the first element with the smallest element
  3. Repeat the step 1 from 2nd element in the array and continue until all elements are sorted
  4. Return the final sorted list.


__author__ = 'Avinash'
def selection_sort(sort_list):
for i in range(len(sort_list)):
smallest_element = min(sort_list[i:])
index_of_smallest = sort_list.index(smallest_element)
sort_list[i], sort_list[index_of_smallest] = sort_list[index_of_smallest], sort_list[i]
print('\nPASS :', i + 1, sort_list)
print ('\n\nThe sorted list: \t', sort_list)
lst = []
size = int(input("\nEnter size of the list: \t"))
for i in range(size):
elements = int(input("Enter the element: \t"))


Selection Sort Algorithm —
Selection Sort Algorithm —


Selection Sort Algorithm in Python – Programming in Python
A simple python program to implement selection sort algorithm. A selection sort is one of the sorting techniques used…



This repo consists code of all the programs discussed at
Never miss a story from, when you sign up for Medium. Learn more
Never miss a story from