473,508 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vector sort: sort() vs stable_sort()

I have a sort statement for vectr like this,

sort(vec.begin(), vec.end(), EleSmaller);

in running I find that the sort procedure falls into an endless loop, by
adding some debug statement I see sort is endlessly doing some
comparision on non-exist number!

Then I replace sort() with stable_sort(), it works fine. This really
confuses me, what might be the reason of such problem? The vector seems
fine, as I can go over it specifically to print out all its elements.

Thanks!

Jul 22 '05 #1
6 2878

"John Black" <bl***@eed.com> wrote in message
news:40***************@eed.com...
I have a sort statement for vectr like this,

sort(vec.begin(), vec.end(), EleSmaller);

in running I find that the sort procedure falls into an endless loop, by
adding some debug statement I see sort is endlessly doing some
comparision on non-exist number!

Then I replace sort() with stable_sort(), it works fine. This really
confuses me, what might be the reason of such problem? The vector seems
fine, as I can go over it specifically to print out all its elements.


Almost certainly that your EleSmaller function is bugged.

EleSmaller must be defined so that

1) a < a is never true

2) a < b and b < c implies a < c

3) its never the case that a < b and b > a are both true

If your EleSmaller function does not satisfy those conidtions then sort can
loop forever. If you are not sure, why not post the code of EleSmaller.

john
Jul 22 '05 #2
John Black <bl***@eed.com> wrote in message news:<40***************@eed.com>...
I have a sort statement for vectr like this,

sort(vec.begin(), vec.end(), EleSmaller);

in running I find that the sort procedure falls into an endless loop, by
adding some debug statement I see sort is endlessly doing some
comparision on non-exist number!

Then I replace sort() with stable_sort(), it works fine. This really
confuses me, what might be the reason of such problem? The vector seems
fine, as I can go over it specifically to print out all its elements.


Perhasps EleSmaller(a,b) and EleSmaller(b,a) are both true for some
a,b in vec? Or in general, there might be loop such that v1 > v2 > vn
but vn > v1.

Whether this loop is discovered can depend on the exact comparisions
made.

Regards,
Michiel Salters
Jul 22 '05 #3
John Harrison wrote:
"John Black" <bl***@eed.com> wrote in message
news:40***************@eed.com...
I have a sort statement for vectr like this,

sort(vec.begin(), vec.end(), EleSmaller);

in running I find that the sort procedure falls into an endless loop, by
adding some debug statement I see sort is endlessly doing some
comparision on non-exist number!

Then I replace sort() with stable_sort(), it works fine. This really
confuses me, what might be the reason of such problem? The vector seems
fine, as I can go over it specifically to print out all its elements.


Almost certainly that your EleSmaller function is bugged.

EleSmaller must be defined so that

1) a < a is never true

2) a < b and b < c implies a < c

3) its never the case that a < b and b > a are both true

If your EleSmaller function does not satisfy those conidtions then sort can
loop forever. If you are not sure, why not post the code of EleSmaller.

john


This makes sense, but I wonder how this applies to my problem: in the vector I
am sorting I know there are some elements are equal to each other, for
example, the vector is a collection of 2 type objects, I want type 1 objects
are before type 2 objects, so in my element comparision function, I do this,

bool EleSmaller(Type o1, Type o2){
if (...o1 is of Type2...){
return false;
}
else{
return true;
}
}

Then if in the vector all the objects are of same type, then that is a>b and
b>a, this violates Strict Less Ordering, but I can not think of another way to
define the comparision. Is stable_sort the only solution here? Does
stable_sort not care Strict Less Ordering?

Thanks.

Jul 22 '05 #4
John Black wrote:

This makes sense, but I wonder how this applies to my problem: in the vector I
am sorting I know there are some elements are equal to each other, for
example, the vector is a collection of 2 type objects, I want type 1 objects
are before type 2 objects, so in my element comparision function, I do this,

bool EleSmaller(Type o1, Type o2){
if (...o1 is of Type2...){
return false;
}
else{
return true;
}
}


When you're working with anything that isn't a numeric type you have to
be extra careful. Lots of people get this sort of thing wrong. Make sure
you've analysed all of the cases.

In this case, it sounds like if o1 and o2 are of the same type, they're
equal. If not, then if o1 is of Type1 (hence o2 is of Type2) o1 < o2,
otherwise o2 < o1.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #5

"John Black" <bl***@eed.com> wrote in message
news:40***************@eed.com...
This makes sense, but I wonder how this applies to my problem: in the vector I am sorting I know there are some elements are equal to each other, for
example, the vector is a collection of 2 type objects, I want type 1 objects are before type 2 objects, so in my element comparision function, I do this,
bool EleSmaller(Type o1, Type o2){
if (...o1 is of Type2...){
return false;
}
else{
return true;
}
}

Then if in the vector all the objects are of same type, then that is a>b and b>a, this violates Strict Less Ordering, but I can not think of another way to define the comparision. Is stable_sort the only solution here? Does
stable_sort not care Strict Less Ordering?

In this case, I don't think you need to use sort() at all. There is
another STL algorithm called partition() that is much better suited to your
purposes.

bool isType1(const Type &o)
{
return (... o is of Type1 ...);
}

Then if you call
partition(vec.begin(), vec.end(), isType1);

all Type1 elements will be placed before all Type2 elements. Note that
partition returns an iterator that points to the end of the range containing
all the elements that evaluate to true; thus if you stored the result of the
above call in the iterator mid, then
[vec.begin(), mid) will contain all Type1 elements and [mid, vec.end())
will contain all Type2 elements. Also, there is a variation
stable_partition which ensures that the relative positions of two elements
that either both evaluate to true or both evaluate to false are unchanged.

Joe Gottman
Jul 22 '05 #6

"John Black" <bl***@eed.com> wrote in message
news:40***************@eed.com...
John Harrison wrote:
"John Black" <bl***@eed.com> wrote in message
news:40***************@eed.com...
I have a sort statement for vectr like this,

sort(vec.begin(), vec.end(), EleSmaller);

in running I find that the sort procedure falls into an endless loop, by adding some debug statement I see sort is endlessly doing some
comparision on non-exist number!

Then I replace sort() with stable_sort(), it works fine. This really
confuses me, what might be the reason of such problem? The vector seems fine, as I can go over it specifically to print out all its elements.

Almost certainly that your EleSmaller function is bugged.

EleSmaller must be defined so that

1) a < a is never true

2) a < b and b < c implies a < c

3) its never the case that a < b and b > a are both true

If your EleSmaller function does not satisfy those conidtions then sort can loop forever. If you are not sure, why not post the code of EleSmaller.

john


This makes sense, but I wonder how this applies to my problem: in the

vector I am sorting I know there are some elements are equal to each other, for
example, the vector is a collection of 2 type objects, I want type 1 objects are before type 2 objects, so in my element comparision function, I do this,
bool EleSmaller(Type o1, Type o2){
if (...o1 is of Type2...){
return false;
}
else{
return true;
}
}

Then if in the vector all the objects are of same type, then that is a>b and b>a, this violates Strict Less Ordering, but I can not think of another way to define the comparision.
bool eleSmaller(Type o1, Type o2)
{
return o1 is of Type1 && o2 is of Type2;
}

That's works doesn't it? Essentially you must return false for the case
where o1 and o2 are equal.

Is stable_sort the only solution here? Does
stable_sort not care Strict Less Ordering?


I think any total sort algorithm would require strict weak ordering.
Otherwise a total ordering isn't necessarilly defined. But I think you have
a strict weak ordering, you just haven't defined it correctly in your
comparison function.

john
Jul 22 '05 #7

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

Similar topics

16
2707
by: Kitty | last post by:
Hi, everyone. Given a vector<int>, what is the fastest way to find out whether there is a repeated element in it? The result is just "true" or "false". Thanks. Kitty
6
3514
by: Xiaozhu | last post by:
say, if you had parallel arrays containing the sales person id, the month, and the sales figures (for that person in that month), sorting on sales figure and preserve the order of figures within...
5
8957
by: Dr. Ann Huxtable | last post by:
Hello All, I am reading a CSV (comma seperated value) file into a 2D array. I want to be able to sort multiple columns (ala Excel), so I know for starters, I cant be using the array, I need...
1
2572
by: Andreas Schmitt | last post by:
Is it possible to do something like this: Team* League; for (int team=0; team<4; team++) { League = new Team;
5
2608
by: Gerhard Menzl | last post by:
Has anyone ever tried to sort a Standard Library container of gcroots? I have run into the problem that somewhere deep in the Library logic (in line 338 of <memory>, to be precise) the destructor...
1
3458
by: Varun Kacholia | last post by:
Hi, I have a question regarding SGI STL sort implementation: In case of equal elements, will they be output in the same order each time I sort? (I understand that it is not a stable sort, and by...
9
1672
by: Timothee Groleau | last post by:
Hi all, My name is Tim, I'm just getting started with C++ and this is my first post to the group. Is there a standard recommended approach to use a vector of pointers along with <algorithm>?...
4
11467
by: boheman | last post by:
Hi, I am wondering if there is a simple and quick way to return the indices of sorted vector. for example, I have a vector<intx containing {5, 2, 3, 0, 2}. I can use sort(x.begin(), x.end(),...
5
6036
by: jeremit0 | last post by:
I'm trying to sort a vector<complex<double and can't figure it out. I recognize the problem is that there isn't a default operator< for complex data types. I have written my own operator and can...
0
7125
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
7328
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7049
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5055
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3199
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1561
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
422
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.