|
-
June 10th, 2005, 10:03 AM
#1
QuickSort
Hi,
I want to quick sort array of strcutures. can anyone provide me a sample code.
Thanks in advance
Vidhu
-
June 10th, 2005, 10:07 AM
#2
Re: QuickSort
 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
-
June 10th, 2005, 10:31 AM
#3
Re: QuickSort
In addition....what kind of array? Old-style C array? STL 'vector'?
-
June 12th, 2005, 08:58 AM
#4
-
June 12th, 2005, 09:07 AM
#5
Re: QuickSort
 Originally Posted by vidhu
Its old style C array
That still leaves Paul's question...
-
June 12th, 2005, 10:49 AM
#6
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
-
June 12th, 2005, 11:17 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|