Question: How do you compute the time complexity for the following function? Show steps and explanation when possible please, thanks. void tower_Of_Hanoi(int num, char peg, char
How do you compute the time complexity for the following function?
Show steps and explanation when possible please, thanks.
void tower_Of_Hanoi(int num, char peg, char peg_to, char peg_aux)
{
//Compute the difference value.
int difference = peg - peg_to;
//Check the validity from the if condition.
if (difference == 1 || difference == -1)
{
//Check the value of num
if (num == 1)
{
//Display the output message.
printf(" The disk 1 moved from %c to %c ", peg, peg_to);
//Ret
return;
}
//Call the function recursively.
tower_Of_Hanoi(num - 1, peg, peg_aux, peg_to);
//Display the output message.
printf(" The disk %d moved from %c to %c ", num, peg, peg_to);
//function call
tower_Of_Hanoi(num - 1, peg_aux, peg_to, peg);
}
//"else" part
else
{
//Call the function recursively.
tower_Of_Hanoi(num, peg, peg_aux, peg_to);
//Call the function recursively.
tower_Of_Hanoi(num, peg_aux, peg_to, peg);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
