# Python Program to perform selection sort

0 like 0 dislike
1.4k views
Python Program to perform selection sort

## 1 Answer

0 like 0 dislike
by Goeduhub's Expert (5.8k points)
edited

Best answer

Selection sort

It is an sorting algorithm in which the array is divided into sorted array and unsorted array and we need to find the minimum element from unsorted array  and put it in a sorted array sequence.

Algorithm:

1. Set minimum element to index 0

2. Search the minimum element in the array

3. Swap with value at minimum index

4. Increment minimum to point to next element

5. Repeat until list is sorted

Example:

a = [64, 25, 12, 22, 11]

here we need to find the minimum element and place it in the beginning from i=0 to 4

11 25 12 22 64

here we need to find the minimum element and place it in the beginning from i=1 to 4

11 12 25 22 64

here we need to find the minimum element and place it in the beginning from i=2 to 4

11 12 22 25 64

here we need to find the minimum element and place it in the beginning from i=3 to 4

11 12 22 25 64

Program Code

def selsort(n):

for i in range(len(n)-1,0,-1):

max=0

for j in range(1,i+1):

if n[j]>n[max]:

max = j

temp = n[i]

n[i] = n[max]

n[max] = temp

n = [78,25,11,29,75,69,45,67]

selsort(n)

print("Sorted array : ",n)

Output

Sorted array : [11, 25, 29, 45, 67, 69, 75, 78]

## For more RGPV/UTMP CSE V Sem Python Lab Experiments Click here

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.