Exact analysis of insertion sort:
Let us assume the following pseudocode to analyse the exact runtime complexity of insertion sort.
T_{j } is the time taken to execute the statement through j^{th} iteration.
The statement at line 4 will execute T_{j }number of times.
The statements at lines 5 & 6 will execute T_{j} - 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.