mystery(int A[1..n], int n) {
// pre: n is a power of 2 for i=1..n {
for i = 1...n {
A[i] = A[i] + 1;
}
if (n>1) mystery(A, n/2);
}
}
I think the worst case, it runs in O(n), am I right?
Edit: This is from another old exam (which has answers for us), but the following algorithm runs in O(n*log n) time (according by the answer). Why so? I though these two should only differ by some constants.
void silly (int n)
if (n>1)
for (int i=0; i<n; i++)
output "looping for fun"
silly(n/2)
for (int i=0; i<n; i++)
output "looping for more fun"
silly(n/2)
for (int i=0; i<n; i++)
output "looping for even more fun"