Python program for Selection sort

Table of Contents

Selection sort

Following is the python program for selection sort. It is great start for beginners and university students to start. Please read the comments it will help you to understand.

You can also check following sort

"""
Author : ITVoyagers (itvoyagers.in)

Date :25th October 2019

Description : Program to show implementation of selection sort using python
"""
def selectionsort(collection):

   for i in range(len(collection)):

      # Find the minimum element in remaining
       min = i

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

   return collection

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

Output :

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

Note : Please note that above program is compatible with Python 3 or higher version

Please check out other practical related to Data structures

3 thoughts on “Python program for Selection sort”

Leave a Comment