tl; dr: Is it possible to efficiently implement quicksort in a doubly linked list? My understanding, before thinking about it, is not, it is not.
The other day I got the opportunity to consider the requirements of the iterator for the basic sorting algorithms. Basic O (N²) - quite simple.
- Bubble sort - 2 forward iterators will do nicely, one is dragged after another.
- Sort insert - 2 bidirectional iterators. One for the item is out of order, one for the insertion point.
- Choosing a sort is a bit trickier, but advanced iterators can do the trick.
Quicksort
introsort_loop in std :: sort (as in the standard gnu / hp library (1994) / silicon graphics (1996)) requires it to be random_access.
__introsort_loop(_RandomAccessIterator __first, _RandomAccessIterator __last, _Size __depth_limit, _Compare __comp)
As I expected.
Now, upon closer inspection, I cannot find the real reason that requires this for quick sorting. The only thing that explicitly requires random_access_iterators is a call to std::__median , which requires the calculation of the middle element. The usual, vanilla quicksort does not calculate the median.
The separation consists of checking
if (!(__first < __last)) return __first;
Not a very useful check for bidirectional gears. However, you should replace this with checking the previous section move (from left to right / right to left) with a simple condition
if ( __first == __last ) this_partitioning_is_done = true;
Is it possible to implement quicksort quite efficiently using only bidirectional iterators? Recursive depth can still be maintained.
NB. I have not tried to implement a real implementation yet.
c ++ algorithm quicksort stl-algorithm
Captain giraffe
source share