As per my logic selection sort find minimum element from the
list and put it in the starting of the list at the end of first iteration. In
the second iteration second minimum element is placed in the second position
and so on.
Source Code:
a=[]
n=int(input("Enter number of elements"))
for i in range(n):
a.append(int(input()))
print("List before sorting")
print(a)
for i in range(n):
for j in range(i+1,n):
if a[i]>a[j]:
a[i],a[j]=a[j],a[i]
print("List after sorting")
print(a)
Output:
Enter number of elements5
5
4
3
2
1
List before sorting
[5, 4, 3, 2, 1]
List after sorting
[1, 2, 3, 4, 5]
No comments:
Post a Comment