Selection sort program in python

           WRITE A PROGRAM FOR SELECTION SORT


def Selection_Sort(MyList):
    #i - outer loop
    #j - Inner loop
    #k - index of the smallest element
    
    for i in range(len(MyList)-1):
        k=i   #i th element is assumed to be smallest
        for j in range(i+1,len(MyList)):
            if(MyList[j]<MyList[k]):
                k=j
        if (k!=i):
            temp=MyList[i]
            MyList[i]=MyList[k]
            MyList[k]=temp
MyList=[12,34,56,2,6,44,32,67,89]
print("Element before swapping:")
print(MyList)
Selection_Sort(MyList)
print("Elements After Sorting:")
print(MyList)


PROGRAM:- 


selection sort in python




OUTPUT:-


selection sort in python




Comments

Post a Comment

Popular posts from this blog

BINARY SEARCH IN PYTHON

LINEAR SEARCH PROGRAM IN PYTHON