Sorting
def insertion_sort(arr):
"""
The time complexity is O(n^2), less efficient than advance sorting algorithms like
quick soer and mergesort.
"""
for i in range(1, len(arr)):
key=arr[i]
j=i-1
while j>=0 and arr[j]>key:
arr[j+1]=arr[j]
j-=1
arr[j+1]=key
return arr
# implementation of binary insertion with bisect
def binary_insertion_sort(arr):
"""
bisect moudle provides a simple and efficient way to perform binary search
on sorted sequences. bisect_left help find the index at which a value
should be inserted into a sorted sequence, while maintaining the order of the sequence.
"""
for i in range(1, len(arr)):
x=arr[i]
j=bisect.bisect_left(arr,x,0,i)
# this is an effectively shifts all the elements in the range [j,i-1] one
# position to the right, to make room for the new element that will be
# inserted at index 'j'
arr[j+1:i+1]=arr[j:i]
arr[j]=x
return arrLast updated
