Click here to Skip to main content
15,889,877 members
Articles / Desktop Programming / MFC
Article

QuickSort enabled CArray template class

Rate me:
Please Sign up or sign in to vote.
4.25/5 (6 votes)
16 Mar 2000 156.6K   3.2K   30   17
A CArray derived class that provides quick and convenient sorting
  • Download demo project - 15 Kb
  • Download source files - 2 Kb
  • This is an updated version - thanks to Donald R. Kichline.

    Motivation

    From time to time, I need to sort arrays containing custom data types, structures, or simple data types provided by C++.  Because I use MFC and CArray, I was looking for a nice solution.  Here it is - my CQArray template class that includes a quick sort method.  In the first version I just provided array template with quick sort method implemented.  This is updated version, with function template too.  Good news, this release is faster than first one, and of course, faster than qsort() function.  Second one is, I extended it by QuickSort() function template.

    Description of solution

    Sorry, I don't describe the quick sort algorithm.  You should be aware that the quick sort algorithm is the fastest sorting algorithm.  It is recursive, and based on partitioning the input array by some element.

    In this release is new function template - QuickSort().  This template is also used by CQArray template.  Therefore, now it is possible to sort any array, not just a CArray like array.

    I kept it as simple as possible.  With CQArray, you can construct any template class.  After construction, by simply calling QuickSort(), you will sort the array.  The function template QuickSort() works the same way.

    Description of CQArray template

    CQArray declaration and implementation:

    template <class T, class PT> class CQArray : public CArray <T, PT>
    {
    public:
      void QuickSort(BOOL bAscending = TRUE);
    };
    
    template <class T, class TP> void CQArray<T,TP>::QuickSort(BOOL bAscending/* = TRUE*/)
    {
      if (GetSize() > 1) 
      {
         ::QuickSort(GetData(),GetSize(),bAscending);
      }
    }

    Above you can see whole CQArray template.  One and only method is QuickSort(), parameter bAscending means sorting ascending (if TRUE), or descending.  Note, this method calls QuickSort() function.

    Description of function template QuickSort

    Declaration of QuickSort() function:

    template <class T> BOOL QuickSort(T *pArr, int iSize, BOOL bAscending = TRUE);

    Implementation is quite longer, please see to sources.  First parameter is address of array to sort.  Second one is size of this array.  Last one has same meaning like CQArray::QuickSort() parameter.  Function returns TRUE if success, otherwise it returns FALSE.  However, only one error can occur - bad parameters.

    Usage

    Usage of CQArray is similar to a CArray:

    ...
    CQArray <int, int &> intArr;
    ...
    // do some stuff to fill array
    ...
    intArr.QuickSort();
    ...

    Usage of QuickSort() function:

    int data[100];
    QuickSort(data,100);
    

    Note, if you want to sort a custom type, you'll need to overload the operators '<' and '>'.

    Demo project

    I provide simple demo project.  You can see usage of template class and function and compare performance of QuickSort() function with qsort() function.

    qarray.jpg (19721 bytes)

    You can set array size, type of elements in array and direction of sorted array.  My 'user defined type' is simple structure.

    Comparision with qsort

    As you can see on previous picture, in demo program you can sort array in three ways.  First one is with CQArray template, second one QuickSort() function template and last one is qsort() function.  Time bellow is in seconds.  My templates are faster than qsort() function.  It is not every time seven times faster, sometimes it is more or less.  It is based on array size, a larger array produces a better ratio.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    Software Developer (Senior)
    United Kingdom United Kingdom
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    GeneralLicense Pin
    Member 138541815-Sep-10 21:31
    Member 138541815-Sep-10 21:31 
    QuestionTerm of use for Qarray.h Pin
    ofe26-Aug-09 4:30
    ofe26-Aug-09 4:30 
    GeneralPointers Pin
    Dean Wyant5-Dec-08 9:03
    Dean Wyant5-Dec-08 9:03 
    QuestionDoes not sort records? Pin
    al25004-Jul-07 8:34
    al25004-Jul-07 8:34 
    AnswerRe: Does not sort records? Pin
    Dean Wyant5-Dec-08 10:55
    Dean Wyant5-Dec-08 10:55 
    Questionvisual studio 2005 Pin
    alexch00729-May-07 1:31
    alexch00729-May-07 1:31 
    Generalqsort+bsearch Pin
    itkid20-Apr-05 20:40
    itkid20-Apr-05 20:40 
    Generalnice one thanks Pin
    Hirosh28-Feb-03 2:10
    Hirosh28-Feb-03 2:10 
    GeneralCArray use Pin
    Manal E. Helal3-Oct-00 1:49
    sussManal E. Helal3-Oct-00 1:49 
    GeneralHelp Pin
    Peter Wong23-Aug-00 23:51
    Peter Wong23-Aug-00 23:51 
    GeneralRe: Help Pin
    Martin Ziacek24-Aug-00 1:46
    Martin Ziacek24-Aug-00 1:46 
    Generalno header file!! Pin
    vivek3-Aug-00 10:35
    vivek3-Aug-00 10:35 
    GeneralRe: no header file!! Pin
    Martin Ziacek3-Aug-00 19:51
    Martin Ziacek3-Aug-00 19:51 
    GeneralIssues with this article Pin
    Ken Nicolson5-Apr-00 20:14
    sussKen Nicolson5-Apr-00 20:14 
    GeneralRe: Issues with this article Pin
    Martin Ziacek6-Apr-00 21:55
    Martin Ziacek6-Apr-00 21:55 
    GeneralRe: Issues with this article Pin
    Ken Nicolson6-Apr-00 22:59
    sussKen Nicolson6-Apr-00 22:59 
    My quick sort implementation is simpler and shorter than qsort() function. Compare also sources, please.

    The C RTL qsort() is larger because it's got special case code to sort sub-arrays of less than about 8 elements using insertion sort, which is faster in these cases.

    When you will compare sources of qsort() and my function, you will see, qsort() is not recursive function. It has statically created array on stack of size 30 for borders of currently sorted intervals. Do You think, is there size limit of sorted array? No, because it is enough. It means, no huge stack usage you can detect in my function.


    No, it's because qsort() selects the larger of the two partitions to sort first, thus the number of recursive calls will be less than 2^30 (2 gigs of chars to sort). With a quick test on your code, I see with 100,000 elements the stack depth grows to 38 to 42 recursive calls. Multiply this by two ints and a T on the stack, and you can see the memory usage is a bit more significant, especially if T is large. Then again, if you have a large T, you should be sorting pointers, not the objects themselves.

    I also tested a bit further the relative performance of this code versus std:sort(), and for 100,000 elements, this code does about 1.4 million assignments and 2.2M comparisons versus 0.9M and 1.9M for std::sort(), for all random data
    GeneralRe: Issues with this article Pin
    Kevin McFarlane8-Oct-04 11:07
    Kevin McFarlane8-Oct-04 11:07 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.