Q. What will be the status of the following list after the First, Second and Third pass of the insertion sort method used for arranging the following elements in descending order?
28, 44, 97, 34, 50, 87
Note: Show the status of all the elements after each pass very clearly underlining the changes.
Answer =
def Bsort(l): count = 0 for i in range(len(l) - 1, 0, -1): for j in range(i): if l[j] > l[j + 1]: l[j],l[j+1] = l[j+1],l[j] print(l) count += 1 if count == 3: break lst = [105, 99, 10, 43, 62, 8] Bsort(lst) print() print("Upto three iterations list will look like: " ,lst)
def Bsort(l):
ReplyDeletecount = 0
for i in range(len(l) - 1, 0, -1):
for j in range(i):
if l[j] > l[j + 1]:
l[j],l[j+1] = l[j+1],l[j]
print(l)
count += 1
if count == 3:
break
lst = [105, 99, 10, 43, 62, 8]
Bsort(lst)
print()
print("Upto three iterations list will look like: " ,lst)
Thank you. : )
DeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )