public inbox for gcc-prs@sourceware.org help / color / mirror / Atom feed
From: Wolfgang Bangerth <bangerth@ticam.utexas.edu> To: nobody@gcc.gnu.org Cc: gcc-prs@gcc.gnu.org, Subject: Re: libstdc++/8195: n-th algorithm (STL) doesn`t work properly Date: Fri, 11 Oct 2002 08:56:00 -0000 [thread overview] Message-ID: <20021011155602.2584.qmail@sources.redhat.com> (raw) The following reply was made to PR libstdc++/8195; it has been noted by GNATS. From: Wolfgang Bangerth <bangerth@ticam.utexas.edu> To: "Belov, Eugeny" <Eugeny_Belov@stl.sarov.ru> Cc: gcc-bugs@gcc.gnu.org, <gcc-gnats@gcc.gnu.org> Subject: Re: libstdc++/8195: n-th algorithm (STL) doesn`t work properly Date: Fri, 11 Oct 2002 10:50:51 -0500 (CDT) > I am understanding that the principle of this algorithm (in simple > terms) is to divide the sequence into 2 parts like this: > smaller elements , n-th element, bigger elements, and in the general > case both parts can be unsorted. Correct. > But the real problem in this case is that smaller elements comes after > n-th ("n-th" is the integer with value 12 - the biggiest of all other > elements), i.e. the sequence after using the nth_element() is "4 4 5 6 6 > 6 9 _12_ 8 10" and in this case I think the n-th algorithm > implementation make a mistake. Why? The signature of the function is void nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last); and you call it like nth_element(a, a+2, a+10); so the result is that elements 0 and 1 will be smaller than the one at position 2, and that elements 3 through 9 will be larger. This is what happens in your output. Regards Wolfgang PS-1: Note that a+2 points to "9" in your example, not "12" as you claim, since indices are zero-based. PS-2: The algorithm sorts such that the values before and after the _pointer_ are smaller and larger, respectively, not those elements that are smaller and larger than the _element pointed to_! I think this is the basic misunderstanding on your behalf. The standard on this reads After nth_element the element in the position pointed to by nth is the element that would be in that position if the whole range were sorted. Also for any iterator i in the range [first, nth) and any iterator j in the range [nth, last) it holds that: !(*i > *j) or comp(*j, *i) == false. ------------------------------------------------------------------------- Wolfgang Bangerth email: bangerth@ticam.utexas.edu www: http://www.ticam.utexas.edu/~bangerth
next reply other threads:[~2002-10-11 15:56 UTC|newest] Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top 2002-10-11 8:56 Wolfgang Bangerth [this message] -- strict thread matches above, loose matches on Subject: below -- 2002-10-11 16:12 paolo 2002-10-11 8:46 Belov, Eugeny 2002-10-11 7:46 Wolfgang Bangerth 2002-10-11 4:16 johnb
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20021011155602.2584.qmail@sources.redhat.com \ --to=bangerth@ticam.utexas.edu \ --cc=gcc-prs@gcc.gnu.org \ --cc=nobody@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).