# Python program to sort elements using selection sort

This program can sort the elements using selection sort elements in python language.

## Logic:

• Select the smallest element in the list. When the element is found, it is swapped with the first element in the list.
• Then the second smallest element in the list is then searched. When the element is found, it is swapped with the second element in the list.
• This process of selection and exchange continues until all the elements in the list have been sorted in ascending order.

## Program:

```n=int(input("Enter the length of list"))#get legth of the list
a=[]                       #declare the list
for i in range(n):         #loop run n times for firat time i automatically initializes ti 0
b=int(input())         #get the list elements
a.insert(i,b)          #insert b into list at index of i
i+=1
for i in range(n):
min=a[i]               #store the ith element to min
for j in range(i,n):   #loop runs n-i times because starting position is i
if min>a[j]:       #check for minimum element
min=a[j]
k=j;           #index of minimum element
j+=1
if k>-1:               #swap only when minimum element found
a[k]=a[i]          #store the ith element into kth index
a[i]=min           #store min value into ith index
k=-1
i+=1
print(a)
```

## To sort elements in array based on their position

### Vignesh

A Computer Science graduate who likes to make things simpler. When he’s not working, you can find him surfing the web, learning facts, tricks and life hacks. He also enjoys movies in his leisure time.
Article Rating
Subscribe
Notify of
1 Comment
Inline Feedbacks