< !- START disable copy paste -->

Tuesday, 5 June 2018

Selection sort


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

Reverse Doubly Linked List

Source Code class Node:    def __init__(self,data):         self.data = data         self.left = None         self.right = None cla...