Selection Sort in Python

Selection Sort

Selection sort uses statistical technique which finds minimum/maximum from a list and puts it at appropriate position. If you consider ascending order the smallest element will be placed at the first position, the second smallest element will be placed at the second position and the third smallest element will be placed at the third position and this process is continued until array is sorted.

Selection Sort in PythonSelection Sort in Python


a=[15,23,40,10,3,35,25,5,30,33]
min=a[0]
for i in range(len(a)):
       min=a[i]
       mindex=i
       for j in range(i+1,len(a)):
               if a[j] < min:
                       min=a[j]
                       mindex=j
       if mindex!=i:
            t=a[i]
            a[i]=a[mindex]
            a[mindex]=t
print(a)


Time Complexity of Selection Sort

The average case complexity of selection sort is O(n2).

Leave a Comment

Your email address will not be published. Required fields are marked *

©Postnetwork-All rights reserved.