Efficient method to sort a CPtrarray?

Posted on 2006-03-22
Medium Priority
Last Modified: 2013-11-20
What is the Efficient method to sort a CPtrarray in c++?
Question by:XPUSR
LVL 49

Accepted Solution

DanRollins earned 200 total points
ID: 16261557
Use qsort... as described here:
    How to use the quick-sort function to sort MFC CArray-derived classes on the MFC and Visual C++ run-time library

You will of course need to sort not on the *value* of the pointer itself, but on the value of the data to which each item points.  You write that logic into your Compare() function.

-- Dan
LVL 16

Expert Comment

ID: 16262271

It depends on what you mean by efficient and a good deal on the type of data. For example, if most of the values are the same except just a few or most of them are already sorted then, believe it or not, bubblesort would be your best bet, with the normal tuning techniques.

Tell us a bit more about the data. Whats in it? How much does it vary? Can you add them one-at-a-time or do you get them in one big block? Etc ...

LVL 45

Expert Comment

ID: 16266432
What is efficient.
The bubble sort is very easy to code but takes a lot of time to sort compared to more advanced techniques.
However, if the array has, say, 8 pointers would the end user really notice it takes 0.001 seconds longer to execute?

(Bubble sort is just go through the array and swap adjacent items if they are not in order.  You keep doing that until no swaps are made.)

Author Comment

ID: 16284890
Thanks for the suggestions. How about conveting it to a vector and using the stl sort. How does this compare? The data in an array of stock performances.

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question