Given a pseudocode like this:
int algorithm(int[] a, i, j)
if i > j
return 0
int n = j - i + 1
int d = n / 4
algorithm(a, i, i+3d-1)
algorithm(a, i+d, j)
return 1
The main cost is the cost given by the two recursive calls. The dimension of this two recursive calls should be in my opinion: 3n/4 for the first one (ignoring the 1) and n/4 for the second one. This means:
T(n) = T(3n/4) + T(n/4) + O(1)
Question 1: is this correct?
Question 2: is T(n) = 2T(3n/4) + O(1)
correct?
In this code, the variables i
and j
are redundant because only their difference is used. So we can substitute j-i+1
with n
everywhere. We can also ignore the argument a
for clarity. The code becomes
int algorithm(n)
if 1>n
return 0
int d= n/4
algorithm(3d)
algorithm(n-1-d)
return 1
Next transformation: substitute d
. We can also discard the returned value !
algorithm(n)
if 1>n
return
algorithm(3(n/4))
algorithm(n-1-n/4)
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.