Exact analysis of insertion sort:
Let us assume the following pseudocode to analyse the exact runtime complexity of insertion sort.
Tj is the time taken to execute the statement through jth iteration.
The statement at line 4 will execute Tj number of times.
The statements at lines 5 & 6 will execute Tj - 1 number of times (one step less) each
Line 7 will execute for (n-1) times
Thus, total time is the sum of time taken for every line multiplied through their cost factor.
Three cases can emerge based on the initial configuration of the input list. First case is where the list was sorted already; second case is the case in which the list is sorted in reverse order and third case is the case where in the list is in random order (unsorted). The best case scenario will emerge while the list is sorted already.