Let's say there is an array A of size n. Which has some elements in it. Target is to print them as fast as possible. The printed order does not matter.
The naive algorithm will be,
print_number (A)
for(i = 0 to n)
print A[i]
And it will have time complexity of O(n)
And what about the below one ?
print_number (A, l, r)
if (l == r) // terminating condition
print A[i]
else
mid = (l+r)/2
print_number (A, l, mid)
print_number (A, mid, r)
This will be O(n)
[EDITED] What if we use parallelism, assume enough processors.
print_number (A, l, r)
if (l == r) // terminating condition
print A[i]
else
mid = (l+r)/2
spawn print_number (A, l, mid) // assign to a new thread / processor
print_number (A, mid, r)
Will it be O(lgn) ? or O(n) ?