In this tutorial, we’ll talk about selection sort, but what’s wrong with bubble sort in python. The problem is in every iteration, first of all, we do multiple iterations because we have to make sure that you get assorted values, and at one point, you swap two values. The thing is, in each iteration, you do multiple swapping, so there’s nothing wrong in traversing from start to end. But swapping consumes the processing power, so we don’t want to do that. We don’t want to consume our CPU power and memory to do that, so swapping should be done only once, and that’s where selection sort
comes into the picture.
We’ll be covering the following topics in this tutorial:
What is Selection Sort in Python
The Selection Sort algorithm
sorts an array by recursively identifying the unsorted list’s minimal value and then replacing it with the first value. It is an in-place algorithm, so no additional lists are required. However, the primary sorting algorithm is still used in systems with limited memory.
Let’s see the visual representation of the algorithm
Explanation
So in such a case, we go from start to end, and we find either the min value or max value. When you depend upon different implementations in this example, we’ll go for the min value, so what you will do. You will go from start to end, and you will find the min value, so in each iteration, you will do only one thing, which is you will find the min value.
so let’s take this value so we have
[5 3 8 6 7 2]
We get the same values which we have taken for bubble sort, so what you will do is you will go from start to end, and you will find the min value, and we all know how to find a min value from the list.
So let’s we will assume that the min value is first value.
min value = 5
but we will start comparing the first value with each element so we compare 5 with 3
if 5 > 3 then
min value = 3
In this case, we know 3 is the min value, and you will compare with the next value and then the next value, and at the end, you will verify that 2 is a min value
min value = 2
So at the end of the first iteration, you’ve got the min value, which is two, and you will swap 5 to the right because we started with five, so now you will swap two with five, and your two goes at the first position.
[2 3 8 6 7 5]
Now from the remaining elements, you will do the same thing, and you will start with the first value, and you will go to the end value. After going from start to end, you got three as a min value, and if you can see in each iteration, we are doing swapping only one, so what you will swap the values with 3 becomes your second value, and now 2 and 3 becomes part of the sorted array.
[2 3 8 6 7 5]
and the remaining value becomes out of an unsorted array. After you will do the same thing with each iteration, you will start swapping the values right.
After the next iteration, you skip three, and then you will now escape the next mini value, which is five in this case, and the eight goes on at the end, you will get a sorted array.
[2 3 5 6 7 8]
So let’s try to implement that and this example. You already have a code here. You can see we have a sorting
Python Program for Selection Sort
def sort(nums): for i in range(5): minpos = i for j in range(i,6): if nums[j] < nums[minpos]: minpos = j temp = nums[i] nums[i] = nums[minpos] nums[minpos] = temp nums = [5,3,8,6,7,2] sort(nums) print(nums)
Output: