Q. Write a program to read a list containing three digit integer only. then write an insertion sort function that sorts the list on the basis of one’s digit of all element.


You can understand by Watching video :-



Answer :-

lst = eval(input("Enter a three digit numbers list = "))
length = len(lst)
for i in range(1,length):
    temp = lst[i]%10
    t = lst[i]
    j = i-1
    while j>=0 and temp<(lst[j]%10):
        lst[j+1] = lst[j]
        j = j -1
    lst[j+1] = t
print(lst)

Output :-

Enter a three digit numbers list = [561,978,357,159,456,789,123]
[561, 123, 456, 357, 978, 159, 789]

>>>

Enter a three digit numbers list = [674,523,895,124,789,451,359]
[451, 523, 674, 124, 895, 789, 359]

>>>

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post