Click to See Complete Forum and Search --> : partial_sort() thing


XZminX
February 18th, 2006, 03:14 PM
I was just wondering how does the partial_sort() places the unsorted elements.
Consider I wanna sort this vector.

2 3 1 5 6 2 4 7 9 12 3 11

from begin() to begin()+5

So the output would be (tested):

1 2 2 3 3 6 5 7 9 12 4 11

and when I put sort from begin() to begin()+6 I get:

1 2 2 3 3 4 6 7 9 12 5 11

Are the elements that dont get into sorted sequence placed randomly?
Because, I see no evidence that their positions have remained unchanged related to each other. If so, the first sort would yield:

1 2 2 3 3 5 6 4 7 9 12 11
and not
1 2 2 3 3 6 5 7 9 12 4 11

Thanks for replies.

Hobson
February 18th, 2006, 03:22 PM
If I get things right, standard says:


25.3.1.3
...

template<class RandomAccessIterator, class Compare>
void partial_sort(RandomAccessIterator first,
RandomAccessIterator middle,
RandomAccessIterator last,
Compare comp);

Effects: Places the first middle - first sorted elements from the range [first, last) into the range [first, middle). The rest of the elements in the range [middle, last) are placed in an unspecified order.


So, order in which 'unsorted' elements are placed, is not determined and implemntation - dependant.

Regards,
Hob

XZminX
February 18th, 2006, 03:30 PM
Thanks.
I must have misread the
The rest of the elements in the range [middle, last) are placed in an unspecified order.
part.