Q. Implement a “bouncing” bubble sort algorithm. In this version of bubble sort, instead of making passes through a list that starts at the beginning and runs through to the end, you should reverse the direction of each pass. That is, if the first pass starts at the beginning of the list and runs through to the end, the second pass would run from the end of the list back to the beginning, and then the third pass would start at the beginning again.
Assume all items in the list are integers.
Answer =
lst = eval(input("Enter a list :-")) for i in range(len(lst)): print(lst) if i % 2 == 0 : for j in range(-1,-len(lst),-1) : if lst[ j ] < lst [ j -1]: lst[ j ] , lst [ j -1] = lst[ j -1] , lst [ j ] else : for j in range(len(lst)-1) : if lst[ j ] > lst [ j + 1]: lst[ j ] , lst [ j + 1] = lst[ j + 1 ] , lst [ j ] print(lst)
Output :-
Enter a list :-[50,41,36,78,1,81]
[50, 41, 36, 78, 1, 81]
[1, 50, 41, 36, 78, 81]
[1, 41, 36, 50, 78, 81]
[1, 36, 41, 50, 78, 81]
[1, 36, 41, 50, 78, 81]
[1, 36, 41, 50, 78, 81]
[1, 36, 41, 50, 78, 81]
>>>
Enter a list :-[23,54,68,10,95,2]
[23, 54, 68, 10, 95, 2]
[2, 23, 54, 68, 10, 95]
[2, 23, 54, 10, 68, 95]
[2, 10, 23, 54, 68, 95]
[2, 10, 23, 54, 68, 95]
[2, 10, 23, 54, 68, 95]
[2, 10, 23, 54, 68, 95]
>>>
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )