Python Program for Selection Sort [Detail explanation with 3 Examples]

This Python tutorial will provide information about what selection sorting in Python is. Also, I will explain with three examples how to write a Python program for selection sorting.

Selection sort is a sorting algorithm in Python that sorts a list by repeatedly finding the minimum element and putting it in ascending order. This process is repeated until the entire Python list gets sorted.

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 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 minimum element (2) with the 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].

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.

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).

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.

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.

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)

Output: Refer to the screenshot below. You can see the result after the Python code gets executed.

2. 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.

Here, is the complete Python code for selection sort using the min() function in Python.

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)

Output:

Sorted array using min function: [11, 12, 22, 25, 64]

The screenshot below provides the result of the Python code for selection sort using the min() function.

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

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

This is the full Python code for selection sort using the max() function.

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)

Output:

Sorted array using max function: [12, 29, 62, 94, 101]

Refer to the screenshot below for the result after executing the Python program.

Conclusion

I hope this Python article provided useful information and gives a clear picture of how to write a Python program for selection sort. I have discussed what selection sort in Python is and the logic of selection sort in Python. Additionally, I have explained three examples of the Python program for selection sort, such as using loops with conditional statements and min() and max() functions.

You may also like to read: