Python Selection Sort

What is The Selection Sort?

Similar to Insertion kind, we start by considering the primary part to be sorted and also the rest to be unsorted. because the formula takes, the sorted portion of the list can grow and also the unsorted portion will still shrink.

Selection kind is regarding picking/selecting the tiniest part from the list and inserting it within the sorted portion of the list.

Initially, the primary part is taken into account the minimum and compared with different components. throughout these comparisons, if a smaller part is found then that’s thought-about the new minimum.

when completion of 1 full spherical, the tiniest part found is swapped with the primary element. This method continues until all the weather is sorted.

Let us see this mistreatment AN example, alist = [5,9,1,2,7,0]

5 is taken into account sorted and also the components nine,1,2,7,0 are thought-about unsorted.

Step 1

5 is the min

5<9: nothing happens

5>1: 1 is new min

1<2: nothing happens

1<7: nothing happens

1>0: 0 is the new min

Swap 0 and 5

alist =  [0,9,1,2,7,5]

Step 2

9 is the min

9>1: 1 is the new min

1<2: nothing happens

1<7: nothing happens

1<5: nothing happens

Swap 1 and 9

alist = [0,1,9,2,7,5]

Step 3

9 is considered min

9>2: 2 is the new min

2<7: nothing happens

2<5: nothing happens

Swap 2 and 9

alist = [0,1,2,9,7,5]

Step 4

9 is considered min

9>7: 7 is the new min

7>5: 5 is the new min

Swap 9 and 5

alist = [0.1.2.5,7,9]

Step 5

7 is considered min

7<9: nothing happen

alist = [0.1.2.5,7,9]

Note: albeit when spherical four we are able to see the list is sorted, there’s no approach for the formula to understand this. thence solely when utterly traversing the whole list, the formula stops.

How to implement The Selection Sort?

Now that you just have a good understanding of what choice kind is, allow us to take a glance at the formula and its code.

Algorithm

Consider the primary part to be sorted and also the rest to be unsorted
Assume the primary part to be the tiniest element.

Check if the primary part is smaller than every one of the opposite elements:
If yes, do nothing

If no, opt for the opposite smaller part as a minimum and repeat step three
After completion of 1 iteration through the list, swap the tiniest part with the primary element of the list.

Now contemplate the second part within the list to be the tiniest then on until all the weather in the list is coated.

NOTE: Once a component is accessorial to the sorted portion of the list, it must not ever be touched and or compared.

Code

def selectionSort(alist):

   for i in range(len(alist)):

      # Find the min element in remaining
       minPosition = i

       for j in range(i+1, len(alist)):
           if alist[minPosition] > alist[j]:
               minPosition = j
                
       # Swap the found min element with minPosition       
       temp = alist[i]
       alist[i] = alist[minPosition]
       alist[minPosition] = temp

   return alist

print(selectionSort([5,2,1,9,0,4,6]))

Output

[0, 1, 4, 2, 6, 5, 9]

Conclusion

Selection kind works best with a little range of components. The worst-case runtime complexness of Insertion kind is o(n2) almost like that of Insertion and Bubble kind. That’s it for this tutorial. Happy Python coding!

Leave a Comment