Selection Sort in Python: Explained with Examples
Updated on Mar 28, 2025 | 4 min read | 5.4k views
Share:
For working professionals
For fresh graduates
More
Updated on Mar 28, 2025 | 4 min read | 5.4k views
Share:
Selection sort in Python is an in-place sorting algorithm. In this sorting algorithm, only one swap happens for every pass through the list. The algorithm works by recursively going through the input array. Each time of recursion returns an item that is smallest in the unsorted sequence and places it in the original array.
The selection sort in Python is an enhanced version of bubble sort. However, for every iteration through the list, only one item is swapped. As the selection sort initiates an iteration, it looks for the largest value in the unsorted list and returns it to the proper location. Hence, after the first iteration, the largest value is in its place, the second iteration puts the second largest value in its place and so on. The process continues till all the items are returned to its location. (n – 1) iterations are required to sort an input array with ‘n’ items. This is because the final item should be in its place after the (n-1)th pass.
Check out our free courses to get an edge over the competition.
def selection _sort(L):
for i in range(len(L) – 1):
min_index = argmin(L[i:])
L[i], L[min_index] = L[min_index], L[i]
Python Code Example for Selection Sort
The minimum value index is returned by the function argmin( ) in the pseudocode shown in Figure 1. A variable ‘I’ is used to track the position of the end of sorted list and the start of unsorted list. Every item in the unsorted sub list is more than any item in the sorted sublist always because the selection sort algorithm starts with zero items in the sorted sub list.
First statement of the code raises the variable’s value by 1 (i + 1). The index of minimum value is determined by the second statement of the code. The third statement performs the swapping of the values. No temporary variables are required for the swapping because the value of the right-hand side is calculated by Python before it is assigned to the left-hand side.
Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.
Let us take an example of the unsorted list shown below to understand how the code in Figure 1 works.
[9, 10, 8, 3, 4]
The sorting algorithm starts with zero items in the unsorted list.
All the items are now placed in the unsorted list. The code looks through each item in the unsorted list to determine the least value. The least value in the list is 3 and it is swapped with 9 in the list.
Now the remaining unsorted elements are compared with each other to find the next lowest element. The lowest element in the remaining items of the unsorted list is 4. It is swapped with 9. Now, the elements in the sorted list are 3, 4.
The process continues till all the elements of the unsorted list are sorted.
This sorting algorithm is simpler and has more performance benefits in cases where auxiliary memory is limited. The major benefit of selection sort is that it performs well with a small list of items. No additional storage is required apart from the one that stores the original list because it is an in-place sorting algorithm.
Check out upGrad’s Advanced Certificate Programme in Cyber Security from IIIT Bangalore
The selection sort in Python is less efficient when executed on a huge list of items. The number of steps required for selection sort is in geometric progression. Hence, its performance worsens with the increase in the length of the list. The performance of bubble sorting algorithm is affected by the initial arrangement of items in the input array before the initiation of the sorting process. Therefore, only a list of a few items that are arranged randomly can give accurate results with the selection sort algorithm.
At upGrad, our Executive Post Graduate Programme in Software Development – Specialisation Cloud Backend Development, offered in collaboration with IIITB, is a 12-15 months program aimed at both freshers and experienced professionals wanting to understand cloud computing in more detail. The course is highly training-oriented and gives you all the practical exposure you require to get started in the world of software development and cloud computing. Check out the course page and get yourself enrolled soon!
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources