Q:

custom sorting function with struct

So, I have a problem with this sorting function, that I wrote to sort struct.
My initial thought was to have a while cycle until there is no changes happening, and inside have for cycle that goes through an array[10], comparing two elements that are next to each other. If next element is larger than previous one, they are exchanged, and iterator thrown back to zero.
Everything kind of works, except for the first element, which is not highest. From second to last, everything is fine.
Any pointers, to where I have made a mistake? Code seems fine to me...
I know I could have used <algorithm>, but I am supposed to write my own function for this.
void izvadaPecRez(Pari masivs[])
{
    Pari temp;
    bool change;
    int i;
    while(change!=false)
    {
        for(i=0;i<10;i++)
        {
            if(masivs[i+1].kopejais>masivs[i].kopejais)
            {
                temp=masivs[i];
                masivs[i]=masivs[i+1];
                masivs[i+1]=temp;
                change=true;
                i=0;
            }
            else
            {
                change=false;
            }
        }
    }

    for(i=0;i<10;i++)
        printone(masivs, i);
}

A:

i=0 is going to happen right before the increment in the for loop runs, so the effect of the statement will be that i==1 in the next loop. The easiest way to get your intended behavior is to just break from the for-loop after you swap the elements (don't worry, it'll only break the for-loop, not the while-loop). You should also just set change=false at the top of the while-loop, rather than setting it whenever you don't swap elements.

