Posts

Showing posts with the label quicksort

Std Sort make std::vector invalid

Image
Clash Royale CLAN TAG #URR8PPP Std Sort make std::vector invalid I found a bug in std::sort and in some implementations of QuickSort in particular, I do not know whether the problem is in the algorithm in general. Essence: When the elements are less than 16 all the norms, because std::sort uses an insertion sort. When there are 17 or more elements, then quick sort is used with a restriction on the depth of recursion from the logarithm of the number of elements, but vector has time to deteriorate at the first __introsort_loop iteration. There is a vector spoilage when many identical elements. Corruption happened by replacement of valid iterators with invalid iterators. Other containers may break too, I did not check. An example for simplicity with a vector of type "int", for more complex objects - crash at the time of sorting, because the invalid object is passed to the comparison function: #include <iostream> #include <vector> #include <algorithm> void quick...