Can i claim this code as one of the methods of insertion sort? | Sololearn: Learn to code for FREE!

+1

Can i claim this code as one of the methods of insertion sort?

def insertion_sort(ar): for i in range(len(ar)): for j in range(i-1): if ar[i] < ar[j]: ar[i],ar[j] =ar[j], ar[i] print(ar) insertion_sort([10,12,1,7,8,6,5])

2/14/2021 12:51:03 AM

Labib Hasan

2 Answers

New Answer

+2

Trace your code with the print of the array after if conditions, and you will see how on the fourth exchange pass your 7 will fly to the end of the array, but should done have been inserted at the sort order.

+2

That makes sense. Yes , thank you . I was trying to implement insertion sort with just the basic understanding of how it works . Don't know what type of sorting it has become now though it is sorting correctly . Do you know ?