sorting using pointer to pointer not working

Hi,

I need to sort an array of structures by a structure member.
Sorting the array elements directly works fine, but is not fast enough.
So I re-wrote the merge sort routine to use an array of pointers pointing to the structures, and want to sort the pointers.
This doesn't work. The pointers don't get sorted to point to the structures in aascending order of the member value.
The sort does change the pointer values, but they don't get sorted by anything as far as I can see.

Below is the part of the code that should be enough to test the problem.

Oh, then you're compiling it as C++...
You seemed to leave out that little detail. In that case, yes, the cast must be there.
But you know, you should really post the real code, test code or not, because this code won't compile under C++ which is what you were compiling with...

Originally Posted by Adak

io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.

Originally Posted by Salem

You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

Are you sure that the copying around of items was what was slow in the first place? How did you measure that, and what type are the items?
I would think that the large number of dynamic memory allocations during the sort would be hurting performance at least as much. Did you realise that all you need to do is allocate a second buffer the same size as the first, up front, and do no furthur allocations whilst it runs?
You can also improve the performance by doing a four-way merge, or rather a "merge and then merge back" approach instead of a "merge and then copy back" approach.
Any particular reason you chose Mergesort? How many items would it typically be sorting?

My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger

Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Elysia,
The real code is a lot bigger.
I initially sorted on the structures themselves, and all was dandy.
The only two things I re-wrote was
- access to the structures (using pointers this time)
- the compare line in msort_test1, which used to read

Code:

if(res_pnt[l].seq_num <= res_pnt[j].seq_num){

and put items in a buffer (b) that was (result *) and worked as expected,
and now reads

Code:

if( (*ptpt[l]).seq_num <= (*ptpt[j]).seq_num ){

puts items in a buffer (b) that is (result **) and doesn't seem to work.

I added the main() part only to make it runnable (I confess I haven't even tested it, so you are probably right it isn't correct, but anything that calls the sort routine will do.)

iMalc,
The actual program measures the time it took to run.
That was initially around a couple of thousand seconds, and is now down to about 30-40 secs (for the largest item count I tested, which was about 900,000).
I chose Mergesort because I needed a fast method for sorting integers up to 4 bytes long.
The number of items to be sorted can be anything between a couple of hundred to almost anything.
Anything that speeds up the sort is most welcome, so thank you for pointing out "merge and then merge back" - I'll check that out.

Elysia,
The real code is a lot bigger.
I initially sorted on the structures themselves, and all was dandy.
The only two things I re-wrote was
- access to the structures (using pointers this time)
- the compare line in msort_test1, which used to read

Code:

if(res_pnt[l].seq_num <= res_pnt[j].seq_num){

and put items in a buffer (b) that was (result *) and worked as expected,
and now reads

Code:

if( (*ptpt[l]).seq_num <= (*ptpt[j]).seq_num ){

puts items in a buffer (b) that is (result **) and doesn't seem to work.

I added the main() part only to make it runnable (I confess I haven't even tested it, so you are probably right it isn't correct, but anything that calls the sort routine will do.)

I'd thought that in your real code, which compiles, you'd have a proper main and would have copy n' pasted that, but I see you merely added some code in the post of yours to make it work or so.
Anyway, remember that main always must return int. It should not return nothing, and not void or anything else, always int. What you did was omit int and thus getting implicit main which is invalid in C++ an deprecated in C89 (in other words - don't use it!).

Originally Posted by Adak

io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.

Originally Posted by Salem

You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

Just to add that quicksort is generally better than mergesort. It is more efficient, even though it has a much worst case scenario complexity (O(n^2) vs O(nlogn)). You could try it with the previous working algorithm and compare

Elysia,
Point taken, and I assure you I haven't committed the sin mentioned in your post (and your sig).
Apart from that, do you have an idea why that sort routine works on items directly, but not on items pointed to?

Elysia,
Point taken, and I assure you I haven't committed the sin mentioned in your post (and your sig).
Apart from that, do you have an idea why that sort routine works on items directly, but not on items pointed to?