Articles

Program to implement selection sort in Python

Program to implement selection sort in Python


In this python programming article, we are going to learn

  • program to implement selection sort in python

Program to implement selection sort in Python

The program to implement selection sort is as follows:

 

# Owner : TutorialsInhand Author : Devjeet Roy

def selection_sort(arr):

    for i in range(len(arr)): 
        min_index = i 
        for j in range(i+1, len(arr)): 
            if arr[min_index] > arr[j]: 
                min_index = j       
        arr[i], arr[min_index] = arr[min_index], arr[i] 

    return arr


if __name__ == "__main__":
    marks = [22,66,43,58,98,42,77,56,66]
    result = selection_sort(marks)

    print("The sorted marks: ", result)

The output of the program is as follows:

 

PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
The sorted marks:  [22, 42, 43, 56, 58, 66, 66, 77, 98]

Few important tips about the program

1. The selection sort algoritm works by finding the smallest element from the unsorted part of the list and putting it at the beginning.

2. The minimum element is picked from the unsorted half of the list and moved to the sorted half of the list.

 

selection sort in python

 


Basic Python Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Devjeet Roy
Full Stack Web Developer & Blockchain Enthusiast
Page Views :    Published Date : Oct 13,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!