0
void function(int A[], int i, int j){
    if (j == i+1) 
        if (A[i] > A[j])
            swap(A,i,j)
        else {
            int k = (j-i+1)/3;
            function(A,i,j-k); 
            function(A,i+k,j);
            function(A,i,j-k); 
        }
}

This piece of code is taken from a past mid-term exam in my Analysis of algorithms class. It was asked of the students to derive a recurrence relation that describes the behaviour of this function. I've seen a few examples on the internet on how this process is done, but I just can't figure out how to apply it on this particular function, the i and j indexes are really confusing to me.

Any ideas?

1
  • Try making a sample array (10 elements or so) and calculate what the actual values of the indexes become. Commented Dec 12, 2015 at 0:50

1 Answer 1

1

Each of the [i,j-k],[i+k,j],[i,j-k] are 2/3 of the [i,j]. So you are dividing your problem to 3 parts when each part is two third of the original size. Therefore your recurrence relation is T(n) = 3*T(n*2/3). You can solve this using Master theorem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.