We help IT Professionals succeed at work.

Efficient method to sort a CPtrarray?

XPUSR
XPUSR asked
on
Medium Priority
705 Views
Last Modified: 2013-11-20
What is the Efficient method to sort a CPtrarray in c++?
Comment
Watch Question

CERTIFIED EXPERT
Author of the Year 2009
Commented:
Use qsort... as described here:
    Q216858
    How to use the quick-sort function to sort MFC CArray-derived classes on the MFC and Visual C++ run-time library
    http://support.microsoft.com/support/kb/articles/Q216/8/58.ASP

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Hi XPUSR,

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 ...

Paul
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
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

Commented:
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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.