Hi,
I have the following pseudo-code for Insertion Sort:
Code:
InsertionSort(Input: integer n, array A)
{
for j = 2 to n {
newnum <- A[j]
i <- j-1
while (i > 0 and newnum < A[i]) {
A[i+1] <- A[i]
i <- i-1
}
A[i+1] <- newnum
}
}
Suppose the number of iterations the while loop runs is r_A(j)=j-1 where r_A(j) denotes the number of elements in A whose value is greater than A[j] but precede it, i.e. the number of elements A[i] so that A[i]>A[j] but i<j for 1<=j<=n.
I would like to determine the time complexity using big theta and r_A(1), r_A(2)...r_A(n) only. I hence wrote the following:
T_IS(A) <= c1*n + c2[r_A(1)+r_A(2)+...+r_A(n)] which I believe to be correct (is it?), yet am not sure how to turn that into a big theta using r_A(1), r_A(2)...r_A(n) as required. Any advice? :-)