Q. Consider the following incorrect implementation of insertion sort that sorts a given list namely List in decreasing order. Explain what is wrong and show how to fix it.
[Hint: You only need to change one line.]
for i in range(1, len(List)):
i2 = i
i1 = i - 1
while i1 >= 0:
if List[i1] > List[i2]:
v = List[i1]
List[i1] = List[i2]
List[i2] = v
i1 -= 1
i2 -= 1
else:
break
Answer =
for i in range(1, len(List)): i2 = i i1 = i - 1 while i1 >= 0 : if List[i1] < List[i2]: v = List[i1] List[i1] = List[i2] List[i2] = v i1 -= 1 i2 -= 1 else: break
Explanation :-
Error in Line 5 there should be greater then (>) symbol if it is not done then program will sort the List in ascending order.
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )