Why is Insertion sort better than Quick sort for small list of elements?

Big-O Notation describes the limiting behavior when n is large, also known as asymptotic behavior. This is an approximation. (See http://en.wikipedia.org/wiki/Big_O_notation) Insertion sort is faster for small n because Quick Sort has extra overhead from the recursive function calls. Insertion sort is also more stable than Quick sort and requires less memory. This question describes … Read more

how do you insert the value in a sorted vector?

The simple answer to the question: template< typename T > typename std::vector<T>::iterator insert_sorted( std::vector<T> & vec, T const& item ) { return vec.insert ( std::upper_bound( vec.begin(), vec.end(), item ), item ); } Version with a predicate. template< typename T, typename Pred > typename std::vector<T>::iterator insert_sorted( std::vector<T> & vec, T const& item, Pred pred ) { … Read more

tech