Original Post
Hey all,
I've been brushing up on my algorithms and data structures lately and today I was messing around with everything in the STL. I had always known about qsort, but it seems like there are some easier functions available if you happen to already be using an STL container such as vector.
Namely something like:
vector myVector;
sort(myVector.begin(), myVector.end());
I'm guessing that sort is implemented as a quicksort, but there is one part that I'm not sure about. The template for sort is
void sort(RandomAccessIterator first, RandomAccessIterator last);
I know that for the basic quicksort (taking first element as pivot) it's worst case is a sorted or reverse sorted list in which it runs at O(n^2). This can be avoided by choosing a random pivot which makes it's expected run time O(n log n). I'm guessing this is what RandomAccessIterator is about, but I'm not familiar with them. If I simply use vector.begin() and vector.end() is it converted to a RandomAccessIterator or do I need to do something special to insure a O(n log n) run time?
Thanks in advance!
I've been brushing up on my algorithms and data structures lately and today I was messing around with everything in the STL. I had always known about qsort, but it seems like there are some easier functions available if you happen to already be using an STL container such as vector.
Namely something like:
vector
sort(myVector.begin(), myVector.end());
I'm guessing that sort is implemented as a quicksort, but there is one part that I'm not sure about. The template for sort is
void sort(RandomAccessIterator first, RandomAccessIterator last);
I know that for the basic quicksort (taking first element as pivot) it's worst case is a sorted or reverse sorted list in which it runs at O(n^2). This can be avoided by choosing a random pivot which makes it's expected run time O(n log n). I'm guessing this is what RandomAccessIterator is about, but I'm not familiar with them. If I simply use vector.begin() and vector.end() is it converted to a RandomAccessIterator or do I need to do something special to insure a O(n log n) run time?
Thanks in advance!