Compute time complexity of Insertion Sort algorithm using Python language implementation
Compute time complexity of Insertion Sort
Step by step
Solved in 3 steps with 2 images
How to calculate the time complexity in seconds/ minutes for this Insertion Sort
# Function to do insertion sort
def insertionSort(arr):
# Traverse through 1 to len(arr)
for i in range(1, len(arr)):
key = arr[i]
j = i-1
while j >=0 and key < arr[j] :
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
arr = [12, 11, 13, 5, 6]
insertionSort(arr)
lst = [] #empty list to store sorted elements
print("Sorted array is : ")
for i in range(len(arr)):
lst.append(arr[i])
print(lst)