Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Would someone please walk this through for me? I\'m just not grasping it Conside

ID: 3597533 • Letter: W

Question

Would someone please walk this through for me? I'm just not grasping it

Consider the C code shown below

mangle(X,n)
    int X[],n;
{
        int i,j;

        for(i = n-1; i > 0; i--)
        {
                for(j = 0; j < i; j++)
                {
                        if( X[j] > X[j+1] )
                        {
                                perturb(X,j,j+1);
                        }
                }
          }
}

How many time will the statement if( X[j] > X[j+1] ) be executed?

Explanation / Answer

it will executed according to the given value of n

lets take an example if n = 5

the i will run from 4 to 1 ( four times)

and j will run 0 to ith times (4 times in first round 3 times in 2nd round 2 times in 3rd round and and 1 times in 4th round)

so the total of 10 times ( 4 + 3 + 2 +1)

for n = 5 it will executed for 10 times

for n = 6 15 times

for nth n*(n-1)/2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote