[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

what is the difference between partial_sort and n_th_element?

puzzlecracker

10/12/2008 12:41:00 AM

NOT SURE WHEN TO USE WHICH
4 Answers

Ian Collins

10/12/2008 1:09:00 AM

0

puzzlecracker wrote:
> NOT SURE WHEN TO USE WHICH

Which what?

--
Ian Collins

Bo Persson

10/12/2008 8:27:00 AM

0

puzzlecracker wrote:
> NOT SURE WHEN TO USE WHICH

Using partial_sort will actually sort the first part of the sequence,
nth_element will not. It just separates the elements in "before" and
"after", but in no particular order.


Bo Persson


PeterAPIIT

10/13/2008 3:11:00 AM

0

On Oct 12, 4:26 pm, "Bo Persson" <b...@gmb.dk> wrote:
> puzzlecracker wrote:
> > NOT SURE WHEN TO USE WHICH
>
> Using partial_sort will actually sort the first part of the sequence,
> nth_element will not. It just separates the elements in "before" and
> "after", but in no particular order.
>


Can you explain with example ?

Zeppe

10/13/2008 10:05:00 AM

0

PeterAPIIT@gmail.com wrote:
> On Oct 12, 4:26 pm, "Bo Persson" <b...@gmb.dk> wrote:
>> puzzlecracker wrote:
>>> NOT SURE WHEN TO USE WHICH
>> Using partial_sort will actually sort the first part of the sequence,
>> nth_element will not. It just separates the elements in "before" and
>> "after", but in no particular order.
>>
>
>
> Can you explain with example ?
>

sequence = [5 6 3 2 4 5 6 7];
|
nth

if I do partial_sort with n = 4, the elements 0,..,4 (before and
including position n) will be sorted, the 5,..,7 will not be
necessarily, that is, a possible outcome is:
[2 3 4 5 5 6 7 6]
|
nth

if I do nth_element, not even the elements strictly before position n
will be sorted, but they will be all less or equal to the nth. A
possible outcome is:
[2 4 5 3 5 6 7 6]
|
nth

If you have full-sort, every element is in its "sorted" position. If you
have partial sort, the first n elements will be in their sorted
position. If you have nth_element, only the nth element is in its sorted
position.

Best wishes,

Zeppe