Question: Reversing a sequence 5 , 4 , 3 , 2 , 1 2 3 int [ ] reverse ( int [ ] data, int low,

Reversing a sequence 5,4,3,2,1
23
int[] reverse(int[] data, int low, int high){
if(low>high) return data;
swap(data[low],data[high]);
return reverse(data,++low,--high);
}
T(n)= c + T(n-2)
=2c + T(n-4)
=3c + T(n-6)
...
= k.c + T(n-2k)
T(1)=1 ; if n is odd; n-2k=1, k = n/2-1/2 ; T(n)= c/2 n c/2+1= O(n)
T(0)=1; if n is even; n-2k =0, k= n/2 ; T(n)= c/2 n +1= O(n)
Explain the steps taken to compute the Time
Complexity (i.e., T(n)) of recursively reversing a sequence

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!