CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: QuickSort

  1. #1
    Join Date
    Apr 2002
    Posts
    35

    QuickSort

    Hi,
    I want to quick sort array of strcutures. can anyone provide me a sample code.

    Thanks in advance
    Vidhu

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: QuickSort

    Quote Originally Posted by vidhu
    Hi,
    I want to quick sort array of strcutures. can anyone provide me a sample code.

    Thanks in advance
    Vidhu
    Any reason why it must be quicksort?

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: QuickSort

    In addition....what kind of array? Old-style C array? STL 'vector'?

  4. #4
    Join Date
    Apr 2002
    Posts
    35

    Re: QuickSort

    Its old style C array

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: QuickSort

    Quote Originally Posted by vidhu
    Its old style C array
    That still leaves Paul's question...

  6. #6
    Join Date
    Apr 2002
    Posts
    35

    Re: QuickSort

    Currently I am using bubble sort. Which is veryslow when the array size is very large.If around 1-2 million or array elements are there. It takes **** of the time to sort it. so i need to fast up the sorting process. So I want to remove bubble sort and replac eit with quick sort

  7. #7
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: QuickSort

    Bubble-sort is O(N²) while quick-sort is O(N log N).

    Now if you have an array of structures, even if it's not a vector you can treat it as one and use STL std::sort.

    std::sort is actually often quicker than qsort because of its nature as a template allows means the compiler can often inline the predicate.

    If you have an array rather than a vector, pass the address of the first element as the first parameter of std::sort, and "one-past-the-last-element" as the second parameter. Thus:

    Code:
    T* begin = &myArray[0]; // = myArray if myArray is an array not a vector
    T* end = begin + arraySize; // = myArray.size() if myArray is a vector.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured