Efficient method to sort a CPtrarray?

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

    Accepted Solution

    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

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

    LVL 43

    Expert Comment

    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

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Title # Comments Views Activity
    Windows Drag & Drop Location 2 79
    Doc'in system (example?) BA 7 64
    java ^ examples 8 48
    canBalance challenge 34 55
    Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
    Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
    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.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now