473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

nth_element() not compatible in VS2005

when I run the following code in vs2005:
int A[] = {7, 2, 6, 11, 9, 3, 12, 10, 8, 4, 1, 5};
const int N = sizeof(A) / sizeof(int);
nth_element(A, A +1, A + N);
copy(A, A + N, ostream_iterator<int>(cout, " "));
I got output: 1,2,3,4,5,6,7,8,9,10,11,12, obviously, it's all sorted.
but sgi stl said the result should be "5 2 6 1 4 3 7 8 9 10 11 12".
http://www.sgi.com/tech/stl/nth_element.html

does it mean that vs2005 not compatible with stl in this function?

Nov 7 '06 #1
7 1789
Hi

zh*********@gmail.com wrote:
when I run the following code in vs2005:
int A[] = {7, 2, 6, 11, 9, 3, 12, 10, 8, 4, 1, 5};
const int N = sizeof(A) / sizeof(int);
nth_element(A, A +1, A + N);
copy(A, A + N, ostream_iterator<int>(cout, " "));
I got output: 1,2,3,4,5,6,7,8,9,10,11,12, obviously, it's all sorted.
but sgi stl said the result should be "5 2 6 1 4 3 7 8 9 10 11 12".
http://www.sgi.com/tech/stl/nth_element.html

does it mean that vs2005 not compatible with stl in this function?
It's not wrong behavior, but it makes me wonder if this is really linear on
average, as required. (Or maybe MS has found an O(n) sorting
algorithm ;-) )

Of course, from one example, you can't tell.

Markus

Nov 7 '06 #2
"Markus Moll" <mo**@rbg.informatik.tu-darmstadt.dewrote in message
news:45***********************@newsspool4.arcor-online.net...
zh*********@gmail.com wrote:
>when I run the following code in vs2005:
int A[] = {7, 2, 6, 11, 9, 3, 12, 10, 8, 4, 1, 5};
const int N = sizeof(A) / sizeof(int);
nth_element(A, A +1, A + N);
copy(A, A + N, ostream_iterator<int>(cout, " "));
I got output: 1,2,3,4,5,6,7,8,9,10,11,12, obviously, it's all sorted.
but sgi stl said the result should be "5 2 6 1 4 3 7 8 9 10 11 12".
http://www.sgi.com/tech/stl/nth_element.html

does it mean that vs2005 not compatible with stl in this function?

It's not wrong behavior, but it makes me wonder if this is really linear
on
average, as required. (Or maybe MS has found an O(n) sorting
algorithm ;-) )
None of the above. Our sort algorithm just does an insertion sort
for small enough sequences, and sometimes it sorts a bit more than
necessary to streamline logic. Its time complexity meets the
requirements of the C++ Standard, and it's generally pretty fast.
Of course, from one example, you can't tell.
Indeed.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Nov 7 '06 #3
I use the following code to test it and It make me sure that VS2005
sort all the element in nth_element.

int A[] = {1,2,3,4,5,6,7,8,9,10,11,12};
const int N = sizeof(A) / sizeof(int);
for(int i=0; i<100; i++)
{
random_shuffle(A, A+N);
nth_element(A, A +rand()%12, A + N);
copy(A, A + N, ostream_iterator<int>(cout, " "));
}

"Markus Moll дµÀ£º
"
Hi

zh*********@gmail.com wrote:
when I run the following code in vs2005:
int A[] = {7, 2, 6, 11, 9, 3, 12, 10, 8, 4, 1, 5};
const int N = sizeof(A) / sizeof(int);
nth_element(A, A +1, A + N);
copy(A, A + N, ostream_iterator<int>(cout, " "));
I got output: 1,2,3,4,5,6,7,8,9,10,11,12, obviously, it's all sorted.
but sgi stl said the result should be "5 2 6 1 4 3 7 8 9 10 11 12".
http://www.sgi.com/tech/stl/nth_element.html

does it mean that vs2005 not compatible with stl in this function?

It's not wrong behavior, but it makes me wonder if this is really linear on
average, as required. (Or maybe MS has found an O(n) sorting
algorithm ;-) )

Of course, from one example, you can't tell.

Markus
Nov 7 '06 #4
Hi

P.J. Plauger wrote:
>It's not wrong behavior, but it makes me wonder if this is really linear
on
average, as required. (Or maybe MS has found an O(n) sorting
algorithm ;-) )

None of the above. Our sort algorithm just does an insertion sort
for small enough sequences, and sometimes it sorts a bit more than
necessary to streamline logic. Its time complexity meets the
requirements of the C++ Standard, and it's generally pretty fast.
Sorry, I haven't thought about that, you're of course right.

Markus

Nov 7 '06 #5
you are right,I just try it. if the size of the sequence is less than
32, it sort all, else it is not sorted.
but I don't know what "sometimes it sorts a bit more than necessary to
streamline logic." mean, would you mind explaining it clearly?

"P.J. Plauger дµÀ£º
None of the above. Our sort algorithm just does an insertion sort
for small enough sequences, and sometimes it sorts a bit more than
necessary to streamline logic. Its time complexity meets the
requirements of the C++ Standard, and it's generally pretty fast.
Of course, from one example, you can't tell.

Indeed.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Nov 7 '06 #6
Markus Moll <mo**@rbg.informatik.tu-darmstadt.dewrote:
(Or maybe MS has found an O(n) sorting
algorithm ;-) )
Well, counting sort/radix sort are O(n), but the constant factors may be
too large for it to be worthwhile in the general case.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Nov 7 '06 #7
<zh*********@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
you are right,I just try it. if the size of the sequence is less than
32, it sort all, else it is not sorted.
but I don't know what "sometimes it sorts a bit more than necessary to
streamline logic." mean, would you mind explaining it clearly?

[pjp] Look at the code in the header. It minimizes work by recursively
partitioning the interval, ignoring any elements partitioned past the
Nth element, until it determines that the Nth element must be in order.
But once the partition size gets under 32 elements, it just sorts whatever
is left rather than do upwards of five more partitionings. Grep the header
for _ISORT_MAX to see other places where we stop being clever and
just do a simple sort on the remainder.

"P.J. Plauger дµÀ£º
None of the above. Our sort algorithm just does an insertion sort
for small enough sequences, and sometimes it sorts a bit more than
necessary to streamline logic. Its time complexity meets the
requirements of the C++ Standard, and it's generally pretty fast.
P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Nov 7 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: craigkenisston | last post by:
Using VS2005 to create an application compatible with framework 1.1 Is this possible ? I like the VS2005 IDE a lot much more than VS2003, however, I'm creating a library that needs to be...
5
by: max | last post by:
Dear all, I did the following analysis to conclude that the following pointer types are not compatible. Please let me know If my analysis and interpretation of the C standard are correct: ...
0
by: Kris | last post by:
Hi.. A few days ago, my project in C#, VS2005 started to have this option activated and when F5, it is rendering all the text labels according to it. The result is that characters are more far...
5
by: Tim Zych | last post by:
Well, I just read this FAQ, which says no. http://msdn2.microsoft.com/en-us/vstudio/aa948854.aspx So my next question is: is MS going to give us a discount to upgrade to VS 2005 so that I can...
56
by: Squishy | last post by:
I tried installing my VS2005 Pro on Vista Ultimate 32 bit RTM today and got errors stating that VS2005 was not compatible with Vista. Microsoft......please pull your finger out of my ass and tell...
1
by: urbansound | last post by:
Hi, I'm having trouble tracking down a PInvoke error in the MySqlDriverCS lib, which is detected even in the author's samples converted to VS v8 .Net 2.0. The calling maze is provided, but the...
3
by: Shaul Dar | last post by:
We are upgrading from VS2003 to 2005. We discovered that the XSD generated in 2005 (.Net 2.0) is not compatible with what was generated in 2003 (.Net 1.1), and this breaks our code, as we have...
3
by: Rick | last post by:
Hello guys!!! I have a little question, i've heard that some people has installed on their machine vs2005 and vs2008, but if you can work vs2005 projects in vs2008 which is the reason to have...
4
by: =?Utf-8?B?UGF1bA==?= | last post by:
I have a vb.net web project that was created in vs2003 and uses crystal reports. I no longer have vs2003 but now have vs2005. I was able to use the wizard and the project builds but when I view...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.