Ex.No. #6a SELECTION SORT (First Model) English



Ex. No. : 6(a)  SELECTION SORT

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

Program:  (Model 1)

def sel_sort(a):
 

    for i in range(len(a)):
        small=a[i]
        pos=i

        for j in range(i+1,len(a)):
            if small>a[j]:
                small=a[j]
                pos=j

        a[pos],a[i]=a[i],a[pos]
a=[]
n=int(input("\n Enter the size:"))
print('\n Enter elements:')

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


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