[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Undefined qsort behavior



qsort is not stable i.e. equal elements can be in any order after sorting.
In your example you'll never know if the first '2' in the source array will
be the first '2' or the second '2' in resulting array. (stable sorting
preserves this order)
You will however always get correct results (equal elements grouped
together)

hope that helps,
Tomek



> -----Wiadomosc oryginalna-----
> Od: hornet@interia.pl [mailto:hornet@interia.pl]W imieniu Miguel A.
> Osorio
> Wyslano: Thursday, 5 December 2002 17:45
> Do: linuxgames@sunsite.dk
> Temat: Undefined qsort behavior
>
>
> Hey people,
>
>
>
> 	It's a fact that we can increase performance in OpenGL by reducing
> state changes, like for example, grouping primitives by material, or
> texture, for example. I'd like to use the C standard lib function qsort
> to do this kind of sorting for me, but it's obvious that I'll have
> repeated entries in my array (more than one primitive with the same
> material/texture). But the qsort man page says: "If  two  members
> compare as equal, their order in the sorted array is undefined". So my
> question is this: this "undefined order" still groups equal elements
> together, or is the undefined *really* undefined ? For instance, if I
> have an array: 4 2 3 1 2 4, do I *always* get: 1 2 2 3 4 4 ?
>
>
>
>
>
> Thanks for your attention,
> Miguel A. Osorio.
>
> ----------------------------------------------------------------------
> Portal INTERIA.PL zaprasza... >>> http://link.interia.pl/f167c
>
>
>


----------------------------------------------------------------------
Portal INTERIA.PL zaprasza... >>> http://link.interia.pl/f167c