|
- Step 1: The for loop executed n times, for i values from 2, 3, .....n+1
- Step 2, 3, and 7: Each executed once for each iteration of the for loop. That is, each n-1 times.
- Step 4: The while loop is executed at most i times, for i = 2, 3, .............., n. That is,
.
- step 5 and 6: Each instruction executed i-1 times, for each i. Hence,
for each instruction.
- Total number of instruction executed in the worst case is
. Hence the time complexity of the algorithm is in .
Similarly, we can analyze space required for the algorithm also. The Insertion sort algorithm has to store all n inputs and using three more variables. So, the space complexity of the algorithm is in .
|
|