#include <stdio.h>
int F(int L[], int p, int q) {
if (p < q) {
int r, f1, f2;
r = (p + q) / 2;
f1 = 2 * F(L, p, r);
f2 = 2 * F(L, r + 1, q);
return f1 + f2;
} else if (p == q) {
return L[p] * L[p];
}else{
return 0;
}
}
int main(void) {
int arr[8] = {1,2,3,4,5,6,7};
printf("%d", F(arr, 0, 7));
}
Someone said the time complexity of this code is O(n)
.
I don't understand it at all...
Isn't it O(logN)
???
Answer: The Big-O complexity is O(N)
Explanation:
The program takes a range of some size (ie q - p + 1
) and cut that range into 2 half. Then it calls the function recursively on these two half ranges.
That process continues until the range has size 1 (ie p == q
). Then there is no more recursion.
Example: Consider a start range with size 8 (eg p=0, q=7
) then you will get
1 call with range size 8
2 calls with range size 4
4 calls with range size 2
8 calls with range size 1
So 7 calls (ie 1+2+4) with range size greater than 1 and 8 calls with range size equal to 1. A total of 15 calls which is nearly 2 times the starting range size.
So for a range size being a power of 2, you can generalize to be
Number of calls with range size greater than 1:
1+2+4+8+16+...+ rangesize/2 = rangesize - 1
Number of calls with range size equal to 1:
rangesize
So there will be exactly 2 * rangesize - 1
function calls when range size is a power of 2.
That is Big-O complexity O(N).
Want to try it out?
#include <stdio.h>
unsigned total_calls = 0;
unsigned calls_with_range_size_greater_than_one = 0;
unsigned calls_with_range_size_equal_one = 0;
int F(int L[], int p, int q) {
++total_calls;
if (p < q) {
++calls_with_range_size_greater_than_one;
int r, f1, f2;
r = (p + q) / 2;
f1 = 2 * F(L, p, r);
f2 = 2 * F(L, r + 1, q);
return f1 + f2;
} else if (p == q) {
++calls_with_range_size_equal_one;
return L[p] * L[p];
}else{
return 0;
}
}
int arr[200] = {1,2,3,4,5,6,7};
int main(void) {
for (int i=3; i < 128; i = i + i + 1)
{
total_calls=0;
calls_with_range_size_greater_than_one=0;
calls_with_range_size_equal_one=0;
F(arr, 0, i);
printf("Start range size: %3d -> total_calls: %3u calls_with_range_size_greater_than_one: %3u calls_with_range_size_equal_one: %3u\n", i+1, total_calls, calls_with_range_size_greater_than_one, calls_with_range_size_equal_one);
}
return 0;
}
Output:
Start range size: 4 -> total_calls: 7 calls_with_range_size_greater_than_one: 3 calls_with_range_size_equal_one: 4
Start range size: 8 -> total_calls: 15 calls_with_range_size_greater_than_one: 7 calls_with_range_size_equal_one: 8
Start range size: 16 -> total_calls: 31 calls_with_range_size_greater_than_one: 15 calls_with_range_size_equal_one: 16
Start range size: 32 -> total_calls: 63 calls_with_range_size_greater_than_one: 31 calls_with_range_size_equal_one: 32
Start range size: 64 -> total_calls: 127 calls_with_range_size_greater_than_one: 63 calls_with_range_size_equal_one: 64
Start range size: 128 -> total_calls: 255 calls_with_range_size_greater_than_one: 127 calls_with_range_size_equal_one: 128
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.