0
void insertion_sort(int *a, int n)
{
int 
for(i=1;i<n;i++)
{
value=a[i];
for(j=i;j>0 && value<a[j-1];j--)
a[j]=a[j-1];

a[j]=value;
}
}



can anyone pls explain me the flow of this code... i am having confusion in understanding the flow..
2
Contributors
4
Replies
8
Views
5 Years
Discussion Span
Last Post by pyTony
0

Do small array, say four elements and write to paper changed values after each line of code.

0

can you tell me that after the control executes this line

a[j]=a[j-1];

where will the control go after that?
will it go for j-- or for

a[j]=value;

0

j>0 && value<a[j-1]

That statement is part of first for executed in this condition and repeating j-- at end of each loop.
a[j] = value is only excecuted after the loop finishes.

0

Formatted in Astyle in Code::Blocks

void insertion_sort(int *a, int n)
{
    int
    for(i=1; i<n; i++)
    {
        value=a[i];
        for(j=i; j>0 && value<a[j-1]; j--)
            a[j]=a[j-1];

        a[j]=value;
    }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.