+ 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])

14th Feb 2021, 12:51 AM
Labib Hasan
Labib Hasan - avatar
2 Respuestas
+ 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.
14th Feb 2021, 2:20 AM
Vitaly Sokol
Vitaly Sokol - avatar
+ 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 ?
14th Feb 2021, 2:34 AM
Labib Hasan
Labib Hasan - avatar