1# another method similar to insertion sort
2
3def insertionSort(arr):
4 for i in range(1, len(arr)):
5 k = i
6 for j in range(i-1, -1, -1):
7 if arr[k] < arr[j]: # if the key element is smaller than elements before it
8 temp = arr[k] # swapping the two numbers
9 arr[k] = arr[j]
10 arr[j] = temp
11
12 k = j # assigning the current index of key value to k
13
14
15arr = [5, 2, 9, 1, 10, 19, 12, 11, 18, 13, 23, 20, 27, 28, 24, -2]
16
17print("original array \n", arr)
18insertionSort(arr)
19print("\nSorted array \n", arr)
20