702 views
0 votes
0 votes
void permute(string elems, int mid, int end)
{
    static int count;
    if (mid == end) {
        cout << ++count << " : " << elems << endl;
        return ;
    }
    else {
    for (int i = mid; i <= end; i++) {
            swap(elems, mid, i);
            permute(elems, mid + 1, end);
            swap(elems, mid, i);
        }
    }
}

How to derive recurrence relation from the above code and what will be the time complexity?

1 Answer

1 votes
1 votes

Given algorithm is  all permutations of a given string.

Suppose You have  String ABC

Then the permutations of string ABC.

ABC ACB BAC BCA CBA CAB

Recurrence relation=n*T(n-1)+n

Time Complexity: O(n*n!)

Related questions

0 votes
0 votes
0 answers
1
Naveen Kumar 3 asked Nov 3, 2018
967 views
Suppose, we have an array of n elements. find the time complexity to search two elements x, y such that:-a) x+y < 100b) x+y 1000Also, state the algorithm/approach for th...
3 votes
3 votes
1 answer
2
sumit_kumar asked Jun 25, 2017
3,146 views
what is time comlexity procedure for following recursive equation by substitution method:T(n)= T(n-1)+T(n-2) , if n>=2 =1 , if n=1; =0 , if n=0.
1 votes
1 votes
1 answer
3
Akriti sood asked Jan 23, 2017
1,305 views
What is the time complexity of the following function foo() void foo() { int i, j; for(i = 1; i <= n ; i++) for(j = i; j <= log(i); j++) printf(“gate”); } what is the...
2 votes
2 votes
1 answer
4