Hi,
I want to quick sort array of strcutures. can anyone provide me a sample code.
Thanks in advance
Vidhu
Printable View
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?Quote:
Originally Posted by vidhu
Regards,
Paul McKenzie
In addition....what kind of array? Old-style C array? STL 'vector'?
Its old style C array
That still leaves Paul's question...Quote:
Originally Posted by vidhu
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
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.