Header Ads Widget

Write a python program for selection sort.

 what is selection sort:-

 In selection sort we repeatedly find the minimum element from unsorted part and putting it at the beginning. In this we have two subarray sorted and unsorted subarray . 

In every cycle we find the minimum from the unsorted subarray and moved it to the sorted subarray .

Code:-


print("Enter the list numbers")
mylist=list(map(int,input().split()))

# logic for the selection sort
l=len(mylist)
for i in range(l):
min=mylist[i]
for j in range(i+1,l):
if min>mylist[j]:
temp=min
min=mylist[j]
mylist[j]=temp
mylist[i]=min
print("Sorted list is : ")
print(mylist)

Output:-


Enter the list numbers
34 2 54 6 78
Sorted list is :
[2, 6, 34, 54, 78]

Recommended Post:-

         MCQs:-


Post a Comment

0 Comments