Ex.No. #6a SELECTION SORT (Second Model) Tamil


Ex. No. : 6(a)  SELECTION SORT

Aim:
To write a Python Program to perform selection sort.

Program:  (Model 1I)

def sel_sort(list1):

    print(list1)

    for i in range(len(list1)):
        min_val=min(list1[i:])
        min_ind=list1.index(min_val)
        list1[i],list1[min_ind] = list1[min_ind],list1[i]
    print(list1)

list1=[]
n=int(input("\n Enter the size:"))
print('\n Enter elements:')

for i in range(n):
    k=int(input())
    list1.append(k)
sel_sort(list1)
print('\n The elements of the list are', list1)


Selection Sort Concept
Original List
 

5
2
1     LSB
3
4


Sorted part      unsorted part
1
2      LSB
5
3
4
 
 

Sorted part                              unsorted part
 

1
2
5
3          LSB
4




Sorted part                                          unsorted part
 

1
2
3
5
        4   LSB
1
2
3
4
5

Sorted List
1
2
3
4
5

Output

Enter the size of the element : 5

Enter the element
5
2
1
3
4
The sorted element of the list are [1,2,3,4,5]

Comments

Popular posts from this blog