Python Program for Selection Sort

When I was working with sorting algorithms, I remember struggling with the logic behind them. At that time, I was working on a small project where I needed to sort a dataset of student grades. I could have used Python’s built-in sort() function, but I wanted to understand how sorting really worked behind.

That’s when I came across Selection Sort. It’s one of the simplest sorting algorithms to understand and implement. While it’s not the fastest algorithm for large datasets, it’s a great starting point for anyone who wants to learn how sorting works step by step.

In this tutorial, I will show you how to implement Selection Sort in Python, explain the theory in simple terms, and walk you through different variations of the algorithm.

Logic of Selection Sort in Python

Here’s a step-by-step explanation of how the Selection Sort algorithm sorts the list “3, 1, 4, 5, 2 ” in ascending order:

  • Start with the list in Python: “3, 1, 4, 5, 2 ”.
  • Find the minimum element in the Python list, which is 1.
  • Swap the place of the minimum element (1) with the first element (3), resulting in “1, 3, 4, 5, 2 ”.
  • Move to the next position and find the minimum element, 2.
  • Swap the place of the minimum element (2) with second element (3), resulting in “1, 2, 4, 5, 3 ”.
  • Repeat this process until the list is sorted completely.
  • After applying the Selection Sort algorithm, the sorted list will be “1, 2, 3, 4, 5 ”.

Selection Sort in Python

Here, I will demonstrate the step-by-step procedure for sorting selection in Python. I have taken a list of temperatures in Fahrenheit, i.e., [42, 30, 35, 53, 27, 50]

Iteration 1:

For the first position in the sorted list, the whole list is passed sequentially from the index [0 to 6]. The first position where 42 is sorted presently, once the whole list is passed, it will be clear that 27 is the lowest value. Thus, replace 42 with 27. After the first iteration, 27 will appear in the first position of the sorted list, like [27, 30, 35, 53, 42, 50].

Selection Sort in Python

Iteration 2:

In the second position, where 30 is present, it is already the second-lowest value in the list. Hence, it will appear in second place in the list; thus, no value swapping occurs.

Iteration 3:

Similarly, in the third position, where 35 is present, it is already the third lowest value in the list. Hence, it will appear in third place, and no values swap occurs.

How to implement selection sort in Python

Iteration 4:

For the fourth position, 53 is present; hence, pass the rest of the list and find the fourth value. While passing, 42 came out to be the fourth least value, which should appear at the fourth place in the Python list; thus, swap 42 with the value present at the fourth position (53).

Python Program for Selection Sort

Iteration 5:

Similarly, pass the rest of the array for the fifth position and find the fifth lowest value in the list. As 50 is the 5th lowest value, it will be placed in the fifth position.

Selection Sort Program in Python

Iteration 6:

At last, the largest value present in the list automatically gets placed at the last position in the list. The resulting list is the final sorted array.

How selection sort works in Python

Here is the complete Python program for the selection sort.

def selectionSort(unsorted_list, size):
    
    for i in range(size):
        min_index = i
 
        for j in range(i + 1, size):
           
            if unsorted_list[j] < unsorted_list[min_index]:
                min_index = j
       
        (unsorted_list[i], unsorted_list[min_index]) = (unsorted_list[min_index], unsorted_list[i])
 
temperatures = [42, 30, 35, 27, 53, 50]
size = len(temperatures)
print('The unsorted list is:',temperatures)
selectionSort(temperatures, size)
print('The list after sorting in ascending order by selection sort is:')
print(temperatures)

I executed the above example code and added the screenshot below.

Selection sort using Python

The selection sort algorithm repeatedly finds the minimum element and places it at the correct position, resulting in a sorted list in ascending order.

1. Python Program for Selection Sort using min() function

You can also do selection sorting in Python using the min() function. Python provides the min() function to return the item with the lowest value or the item with the lowest value in an iterable.

def selection_sort_min(arr):
    n = len(arr)
    for i in range(n):
        min_index = arr.index(min(arr[i:]))
        arr[i], arr[min_index] = arr[min_index], arr[i]

list_of_number = [64, 25, 12, 22, 11]
selection_sort_min(list_of_number)
print("Sorted array using min function:", list_of_number)

I executed the above example code and added the screenshot below.

Selection sort in Python using min() function

Using the min() function in selection sort simplifies finding the smallest element, making the code shorter and easier to understand.

2. Python Program for Selection Sort using max() function

This is another method for selection sort using the max() function in Python. The max() function in Python returns the item with the highest value or the item with the highest value in an iterable.

def selection_sort_max(arr):
    number = len(arr)
    for i in range(number-1, -1, -1):
        max_index = arr.index(max(arr[:i+1]))
        arr[i], arr[max_index] = arr[max_index], arr[i]

list_of_numbers = [94, 29, 12, 62, 101]
selection_sort_max(list_of_numbers)
print("Sorted array using max function:", list_of_numbers)

I executed the above example code and added the screenshot below.

Python program for selection sort using max() function

Using the max() function in selection sort helps place the largest element at the correct position in each iteration, resulting in a sorted array.

Selection Sort may not be the fastest algorithm, but it’s one of the easiest to understand and implement. I’ve used it many times while teaching beginners because it clearly shows how sorting works step by step.

I hope you found this tutorial helpful.

You may also like to read other Python articles:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.