Write a Python  Program to perform selection sort. 

Program Algorithm:
1. Create a function named selection sort
2. Initialise pos=0
3. If alist[location]>alist[pos] then perform the following till i+1,
4. Set pos=location
5. Swap alist[i] and alist[pos]
6. Print the sorted list

Program Code:
def selectionSort(alist):
 for i in range(len(alist)-1,0,-1):
 pos=0
 for location in range(1,i+1):
 if alist[location]>alist[pos]:
 pos= location
 temp = alist[i]
 alist[i] = alist[pos]
 alist[pos] = temp
alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist) 


Program Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]


Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: