The Gateway to Computer Science Excellence
0 votes
133 views

Consider the following C code:

int getNextGap(int gap){
    gap=(gap*10)/13;
    if(gap<1)return 1;
    return gap;
}
void mystery(int a[ ],int n){
    int gap=n;
    bool red=true;
    while(gap!=1||red==true){
        gap=getNextGap(gap);
        red=false;
        for(int i=0;i<(n-gap);i++){
            if(a[i]>a[i+gap]){
                swap(a[i],a[i+gap]);
                red=true;
            }
        }
    }
}

The array $A=\left \{ 9,4,-1,3,5,7,99,-33,104 \right \}$ passed in the above function , and n is number of array elements, then what output  will print at last?


I mostly stuck how bool function working here. Plz help me out, how program executing:(

in Programming by Veteran (117k points)
edited by | 133 views
+1
A={9,4,-1,3,5,7,99,-33,104}

n=9

gap=9

red=TRUE

Program enters loop when either gap!=1 or red is TRUE

1) gap=6, A={9,-33,-1,3,5,7,99,4,104}, red =TRUE

2) gap=4, A={5,-33,-1,3,9,7,99,4,104}, red =TRUE

3) gap=3, A={3,-33,-1,5,4,7,99,9,104}, red =TRUE

4) gap=2, A={-1,-33,3,5,4,7,99,9,104}, red =TRUE

5) gap=1, A={-33,-1,3,4,5,7,9,99,104}, red =TRUE

6) Since red=TRUE loop continues, gap=1, A={-33,-1,3,4,5,7,9,99,104}, red =FALSE since no swaps

LOOP TERMINATES
0

@sakharam

1st swapping for 3 and 9 

right?

I mean 1st time in for loop

for(int i=0;i<(9-6);i++){

if(a[0]>a[3]){

//so 1st swapping should be in 3 and 9

Where am I missing?

+1
for(int i=0;i<(9-6);i++){

if(a[0]>a[0+gap]) i.e.  if(a[0]>a[0+6]) i.e. if(a[0]>a[6])

So we check 9 and 99
0
oh , yes , bad mistake :(

let me chk once more
0
yes, thank u very much :)

is it not a complicated program?
0
If one figures out what each part of program is doing it becomes easy
I felt https://gateoverflow.in/311611/made-easy-test-series-programming-test4-function-call complicated because of the time required to solve it.
0

Yes Sorting is going IF we figure it out two three scan  i dont know why they make such easy concept into complicated (time consuming wasting)

+1
Only correct dry run is necessary
0
@akshat sharmaThe program doesn't sort an array.

Example 5,4,3,2,1,0,10,9,8
0

@sakharam

In that example , it also gives sorted sequence

but i=1 loop execute one more time.

Isnot it??

If one figures out what each part of program is doing it becomes easy

which part in this program u divide?? 

mostly in scanning loop part there are chances of mistake

right??

+1
Yes you are right

I forgot that it was OR condition in while loop
+1
Here I would say two parts, the function and the statements inside if branch. Also the condition in while loop
0
There is no bool datatype in C then how the code will execute?

Please log in or register to answer this question.

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,647 questions
56,465 answers
195,380 comments
100,303 users