#Insertion sort ar = [34, 42, 22, 54, 19, 5] for i in range(1, len(ar)): while ar[i-1] > ar[i] and i > 0: ar[i-1], ar[i] = ar[i], ar[i-1] i -= 1 print(ar)
Here is what the above code is Doing:
1. Iterate through the list, starting at index 1.
2. If the element at index i-1 is greater than the element at index i, swap them.
3. If the element at index i-1 is less than the element at index i, do nothing.
4. Repeat steps 2 and 3 until the element at index i-1 is less than the element at index i.
5. Repeat steps 1-4 until the list is sorted.