# sorting of structure

struct contestant
{

``````int prob,time;
bool operator < (const struct contestant &c ) const
{
if( this->prob > c.prob ) return 1;
else if( this->prob == c.prob)
{
if( this->time <= c.time ) return 1;
return 0;
}
return 0;
}
``````

} con[1000];

when i applly library sort function sort(con,con+size) when storing elements in structure from 0…size-1 it works fine when same sort function used like sort(con+1,con+size+1) when storing elements in structure con from 1…size works incorrectly… any help please…

You can sort from a particular position till the end of the array or less than that but if you try sort beyond the size of the array due to garbage values that are stored in the memory you will face problems

Let the array be 1 4 3 2 0
now beyond this array some garbage values are stored inside the memory locations
like

G1 G2…1 4 3 2 0…G3 G4… ( G -> garbage value)

So if you sort from x to y where 0<=x<=y<(size-1) there wont be any effect of these garbage values on the sorting and elements from index x to y are sorted using sort(con+x,con+y); if you want to sort the entire array then x = 0 and y = size-1.

but if x < 0 or y > size-1 now sort function considers garbage values stored in the memory and sorts according to them which will mess up with the elements of the array.

i want to mention that if i store value con[1…size] and apply sort(con+1,con+size+1) it works wrong while if i store value con[0…size-1] and call sort(con,con+size) it works ok.

thats interesting It must work

//