473,386 Members | 1,973 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.

Partially Sort Struct Vector

hi,
I need to partially sort a struct vector and I have written a function that looks like:

void AxisSort(int start, int end){
sort(prims.begin() + start, prims.begin() + end-start+1, compare);
}

"prims" here is a struct vector

The function works fine when start=0 and end=prims.size()-1
However, with other values the function simply stuck there as if entering an infinitive loop
I wonder if the sort function can only be used to sort the whole vector, but I cannot confirm if that is really the case.

Another choice is qsort, and it works. However, theoretically it is slower than sort. Does any one have better idea to partially sort a struct vector? Thanks.
Oct 17 '07 #1
3 1552
Banfa
9,065 Expert Mod 8TB
If start is the index of the first item to sort and end is the index of the last item to sort then

prims.begin() + end-start+1

is wrong and will only work correctly if start == 0 and in the case (end < 2 * start - 1) then the end point will be before the start point and this is likely to cause trouble.
Oct 17 '07 #2
Thanks a lot! It works now.

And the interesting is that, contrary to what I had expected, my program runs faster with qsort than with sort.
Oct 17 '07 #3
Banfa
9,065 Expert Mod 8TB
Thanks a lot! It works now.

And the interesting is that, contrary to what I had expected, my program runs faster with qsort than with sort.
np :-)

The speed of a sort is not entirely dependent on the algorithm used but is also dependent on the data input into it, i.e. the items to be sorted and the order they are already in.

The question which is the fastest sorting algorithm is not valid, you have to ask which is the fastest sorting algorithm given this set of data to be sorted. Obviously you do not want to implement multiple sorting algorithms or the complex algorithm required to decide which is the right one (you would probably spend more time deciding which is the right algorithm than you would actually doing the sort). So in a real life situation you select an algorithm that is fast given the data you are most commonly going to have to sort and accept that now and then you will get cases where you algorithm will not be efficient.

Whatevery you decide to do sorting wise remember that choosing the Bozo Sort algorithm is always the wrong choice :D
Oct 17 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: {AGUT2} {H}-IWIK | last post by:
Guys, I have these headers: #include <stdlib> #include <math> #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <string> #include <algorithm>
9
by: JasBascom | last post by:
say i had 97456 and 23456 is there already a sort function to check which one begins with the smaller number rearrange it. in this case the bottom number should clearly be rearranged to the...
2
by: William Payne | last post by:
Hello, I have two structs: struct FileEntry { FileEntry(const char* name, const char* type, std::size_t file_size, HICON icon) : m_file_size(file_size),
8
by: b83503104 | last post by:
In matlab, the sort function returns two things: =sort() a = 5 7 8 b = 1 3 2 where a is the sorted result, and b is the corresponding index. Is there C++ code available to...
7
by: Ireneusz SZCZESNIAK | last post by:
I want to sort a vector with the std::sort function. There are two functions: one with two arguments, the other with three arguments. I am using the one with three arguments. I noticed that...
8
by: markww | last post by:
Hi, If I have a vector of structs like this: struct IMAGE { unsigned char *pPix; string strName; int nNumber; };
22
by: sandy | last post by:
I am trying to make a simulated directory structure application for a course. I am using a Vector to store pointers to my Directory objects (as subdirectories of the current object). In my...
5
by: fade | last post by:
Good afternoon, I need some advice on the following: I've got a class that has a member std::vector<CStringm_vFileName and a member CString m_path; The vector contains a bunch of filenames with...
4
by: Santosh Nayak | last post by:
Hi, Is it possible to sort the array of struct based on the data members. e.g. struct { int a ; float b ; char c ; } TEMP ;
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
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,...
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.