Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

Selection Sort In Python

2 min read

In a nutshell, sorting is nothing but arranging data in a particular fashion. Selection sort is a popular sorting algorithm.

In Selection sort, First and foremost the list is divided into two parts left part being the sorted which is initially empty and right part unsorted which at the very beginning is the entire list then at each step elements are recursively compared and swapped depending on the condition till the list is entirely sorted.

While sorting a list in ascending order first, we need to go through the entire list and then find the minimum element and swap it with the element in the leftmost. Next, we need to find the second smallest element and swap it with the 2nd element of the list, and this keeps on going until the entire list is sorted.

Similarly, for sorting a list or array in descending order, we need to find the maximum element and swap it with the first element and so on.

Since at each step we are selecting the next element for the sorted list hence named selection sort.Below selectionSort in Python

Algorithm For Selection Sort In Python

  • Step 1Select the minimum element of the list and swap it with the first element (for Ascending order).
  • Step 2: In every comparison, if any element is found smaller than the selected element, then both are swapped.
  • Step 3: Repeat the same procedure with the element in the next position in the list until the entire list is sorted.

Python Program For Selection Sort

# Sorting function

def selection_sort(l):
    for start in range(len(l)):
        min_pos = start
        for i in range(start, len(l)):
            if l[i] < l[min_pos]:
                min_pos = i
        # swap values
        (l[start], l[min_pos]) = (l[min_pos], l[start])

# Test input
l = [54, 26, 93, 17, 77, 31, 44, 55, 20]


[17, 20, 26, 31, 44, 54, 55, 77, 93]


In the above program, the start position is initially 0  at each iteration it keeps increasing till the limit len(l), at every iteration when the nested loop finds a smaller element than the element at the position start the values get swapped this continues until the entire list is sorted.

Analysis Of Selection Sort

For an unsorted sequence of length n the algorithm requires n step for the first scan then at each iteration it reduces by 1. Mathematically this can be expressed as,

T(n) = n + (n-1) + (n-2) + ....+ 1 = n(n+1)/2 = O(n2)

The above expression concludes that this algorithm is proportional to n2. Therefore for a given list of size n, the time complexity of selection sort can be expressed as,

Worst Case Time Complexity [ Big-O ]: O(n2)

Best Case Time Complexity [Big-omega]: O(n2)

Average Time Complexity [Big-theta]: O(n2)

Space Complexity: O(1) 


Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read