Derivation of a Quadratic Execution Time Function
This routine below sorts a testArray of size N into ascending order by starting i at 1, j at 0 and
marching j over to i, one step at a time, each time comparing the i'th element to j’th element, thus
eventually (in the inner, or j, loop) comparing the i’th element to each element above it. Any
time it finds two elements of the array out of order, it swaps them. So when j reaches i, the first i
elements of the array are thus in order. Then i is advanced by one, j is reset to zero and then,
using j, the new i'th element is compared to all the elements above it. Once i reaches N, the
entire array is thus in order. The real code, whose actual execution times we saw in class, looks
like this:
void sort(int testArray[ ], int N)
{
int i, j, temp;
for (i=1; i= N) return; /* All done */
line4: j = -1; /* Next line kicks j up to the proper starting point */
line5: j++; /* Increment j */
line6: if (j >= i) goto line2; /* Increment i; restart the inner j loop */
line7: if (testArray[i] < testArray[j])
{ /* I f they’re out of order, swap the i’th and j’th elements */
line8: temp=testArray[i];
line9: testArray[i]=testArray[j];
line10: testArray[j]=temp;
}
line11: goto line5; /* Continue in the inner loop, move to next j */
}
Derivation of a Quadratic Execution Time Function
Execution time analysis: The execution time of each line is constant; the constant will vary
from one compiler to another and from one machine to another. But on a given machine, with a
given compiler, the execution time of a given line is constant, call it ci for line i. Now what we
need to know is how often each line executes, call that ni Then the total execution time will be
ci ∙ ni So as a function of N, the size of the array, how often does each line execute?
line1 executes once, regardless of the value of N.
line2 executes N times
line3 executes N times
line4 executes N-1 times
line5 is the first interesting case (the "goto" in line11 causes line5 to execute many more
times than line4, above). Look closely and see that line5 executes once when i=0,
twice when i=1, three times when i=2, … N times when i=N-1, which is as far as i goes in
here (because of line3). So the total number of times line5 executes is 1+2+3+ … +N, or
N
I presume you’ve seen, or better yet, done, a
i = N (N+1)/2 = N2 /2 + N/2 proof of this in CS222 or some other math class.
i =1
line6 executes whenever line5 executes, so it too is N2 /2 + N/2
line7 does not execute when i=0, executes once when i=1, twice when i=2, … N-1 times when
i=N-1. (The “if” statement in line6 always executes once more than line7.) So
N-1
line7 executes i = N(N-1)/2 = N2 /2 - N/2 times.
i =1
line8 executes N2 /2 - N/2 times in the worst case, namely, when the array is initially sorted
ass-backwards from the way we want it and testArray[i] < testArray[j] every
time we check it in line7
lines9-11 the same as line8, obviously, also N2 /2 - N/2
line12 executes once, regardless of the value of N
So for the total execution time,
ci ∙ ni = c1 + c2∙N + c3∙N + c4∙ (N-1) + c5 ∙ (N2 /2 + N/2) + c6 ∙ (N2 /2 + N/2)
+ c7 ∙ (N2 /2 - N/2) + c8∙ (N2 /2 - N/2) + c9 ∙ (N2 /2 - N/2) + c10∙ (N2 /2 - N/2)
+ c11 ∙ (N2 /2 - N/2) + c12
= (c5+c6+c7 +c8+c9 +c10+c11 ) ∙ N2 /2
+ ( c2+c3+c4+c5/2+c6/2-c7/2-c8/2-c9/2-c10/2-c11/2) ∙ N
+ (c1-c4+c12)
= a∙N2 +b∙N+d, a standard quadratic function of N, where
a = (c5+c6+c7 +c8+c9 +c10+c11 )/2
b = c2+c3+c4+(c5+c6-c7-c8-c9-c10-c11)/2
d = c1-c4+c12