Click here to Skip to main content
Licence 
First Posted 16 Mar 2000
Views 126,568
Bookmarked 29 times

QuickSort enabled CArray template class

By Martin Ziacek | 16 Mar 2000
A CArray derived class that provides quick and convenient sorting

1
1 vote, 16.7%
2
1 vote, 16.7%
3
1 vote, 16.7%
4
3 votes, 50.0%
5
4.00/5 - 16 votes
μ 4.00, σa 2.21 [?]
  • 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

    About the Author

    Martin Ziacek

    Software Developer (Senior)

    United Kingdom United Kingdom

    Member


    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    GeneralLicense PinmemberMember 138541822:31 15 Sep '10  
    QuestionTerm of use for Qarray.h Pinmemberofe25:30 6 Aug '09  
    GeneralPointers PinmemberDean Wyant10:03 5 Dec '08  
    QuestionDoes not sort records? Pinmemberal25009:34 4 Jul '07  
    AnswerRe: Does not sort records? PinmemberDean Wyant11:55 5 Dec '08  
    Questionvisual studio 2005 Pinmemberalexch0072:31 29 May '07  
    Generalqsort+bsearch Pinmemberitkid21:40 20 Apr '05  
    Generalnice one thanks PinmemberHirosh3:10 28 Feb '03  
    GeneralCArray use PinsussManal E. Helal2:49 3 Oct '00  
    GeneralHelp PinsussPeter Wong0:51 24 Aug '00  
    GeneralRe: Help PinsussMartin Ziacek2:46 24 Aug '00  
    Generalno header file!! PinsussVivek11:35 3 Aug '00  
    GeneralRe: no header file!! PinsussMartin Ziacek20:51 3 Aug '00  
    GeneralIssues with this article PinsussKen Nicolson21:14 5 Apr '00  
    GeneralRe: Issues with this article PinsussMartin Ziacek22:55 6 Apr '00  
    GeneralRe: Issues with this article PinsussKen Nicolson23:59 6 Apr '00  
    GeneralRe: Issues with this article PinmemberKevin McFarlane12:07 8 Oct '04  

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

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

    Permalink | Advertise | Privacy | Mobile
    Web01 | 2.5.120209.1 | Last Updated 17 Mar 2000
    Article Copyright 2000 by Martin Ziacek
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid