The function used for insertion sort has an error because of which the visualizer keeps swapping elements like bubble sort.
while True:
ascending_sort = i > 0 and lst[i - 1] > cur and ascending
descending_sort = i > 0 and lst[i - 1] < cur and not ascending
if not ascending_sort and not descending_sort:
break
lst[i] = lst[i - 1]
i = i - 1
#lst[I] = cur keeps swapping the adjacent elements
lst[i] = cur
draw_lst(draw_info, {i: draw_info.RED, i - 1: draw_info.GREEN}, True)
yield True
The above code snippet is present in the code base. It should be replaced by last[I] = cur after the while loop when the cur is at the position it should be inserted at.
def insertion_sort(draw_info, ascending=True):
lst = draw_info.lst
for i in range(1, len(lst)):
cur = lst[i]
while True:
ascending_sort = i > 0 and lst[i - 1] > cur and ascending
descending_sort = i > 0 and lst[i - 1] < cur and not ascending
if not ascending_sort and not descending_sort:
break
lst[i] = lst[i - 1]
i = i - 1
draw_lst(draw_info, {i: draw_info.RED, i - 1: draw_info.GREEN}, True)
yield True
lst[i] = cur
return lst