+ 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 Answers
+ 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 ?