Q. Consider the following unsorted list: 105, 99, 10, 43, 62, 8. Write the passes of bubble sort for sorting the list in ascending order till the 3rd iteration.

 

Answer =


lst = eval(input("Enter a list :-"))

for j in range ( 3 ):
    for i in range ( 2) :
        if  lst [ i ] > lst [ i + 1 ] :
            lst [ i ] , lst [ i + 1 ] =  lst [ i + 1 ] , lst [ i ]

print(lst)

Output  :-

Enter a list :-[105, 99, 10, 43, 62, 8]
[10, 99, 105, 43, 62, 8]
>>> 


Enter a list :-[9,8,7,6,5,4,3,2,1]
[7, 8, 9, 6, 5, 4, 3, 2, 1]
>>> 




5 Comments

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

Post a Comment

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

Previous Post Next Post