The standard algorithms for sorting containers are std::sort and std::stable_sort.
These snippets include extra functions describing how these tasks can be implemented with many other classic
sorting algorithms. They are mostly relevant for people studying sorting algorithms for the first time.
1
std::vector<int>v={5,4,9,8,6,3};
12345
// - std::sort is what you would use daily// - don't implement your own version unless you're studying the algorithmsstd::sort(v.begin(),v.end());std::copy(v.begin(),v.end(),std::ostream_iterator<int>{std::cout," "});std::cout<<'\n';
1234
// Elements that compare equal don't change their relative positionsstd::stable_sort(v.begin(),v.end());copy(v.begin(),v.end(),std::ostream_iterator<int>{std::cout," "});std::cout<<'\n';
1234
// Elements that compare equal don't change their relative positionsstd::stable_sort(v.begin(),v.end());copy(v.begin(),v.end(),std::ostream_iterator<int>{std::cout," "});std::cout<<'\n';
1 2 3 4 5 6 7 8 9101112
template<classIt,classC>voidselection_sort(Itfirst,Itlast,Ccomp){// For each positionfor(autoit=first;it!=std::prev(last);++it){// Swap the current with the min_elementstd::iter_swap(it,std::min_element(it,last,comp));}}template<classIt>voidselection_sort(Itfirst,Itlast){// If no comparison function is provided, use `std::less`selection_sort(first,last,std::less<std::decay_t<decltype(*first)>>());}
1 2 3 4 5 6 7 8 9101112
template<classIt,classC>voidinsertion_sort(Itfirst,Itlast,Ccomp){// For each positionfor(autoi=std::next(first);i!=last;++i){// Push the element to the sorted subarray to the leftrotate(upper_bound(first,i,*i,comp),i,i+1);}}template<classIt>voidinsertion_sort(Itfirst,Itlast){// If no comparison function is provided, use `std::less`insertion_sort(first,last,std::less<std::decay_t<decltype(*first)>>());}
1 2 3 4 5 6 7 8 91011121314151617
template<classIt,classC>voidmerge_sort(Itfirst,Itlast,Ccomp){// While more than 1 elementif(last-first>1){Itmiddle=first+(last-first)/2;// Sort left-hand sidemerge_sort(first,middle,comp);// Sort right-hand sidemerge_sort(middle,last,comp);// Merge left and right-hand sidesinplace_merge(first,middle,last,comp);}}template<classIt>voidmerge_sort(Itfirst,Itlast){// If no comparison function is provided, use `std::less`merge_sort(first,last,std::less<std::decay_t<decltype(*first)>>());}
123456789
template<typenameT,classC>constexprTmedian(Tt1,Tt2,Tt3,Ccomp){return(comp(t1,t2))?((comp(t2,t3))?t2:((comp(t1,t3))?t3:t1)):((comp(t1,t3))?t1:((comp(t2,t3))?t3:t2));}template<classIt>voidmedian(Itfirst,Itlast){// If no comparison function is provided, use `std::less`median(first,last,std::less<std::decay_t<decltype(*first)>>());}
1 2 3 4 5 6 7 8 910111213141516171819202122
template<classIt,classC>voidquick_sort(Itfirst,Itlast,Ccomp){// If there is more than 1 element to sortif(first!=last&&std::next(first)!=last){Itmiddle=first+(last-first)/2;// Choose a pivot based on the median elementautopivot=median(*first,*middle,*std::prev(last),comp);// Partition the vector based on the pivotItsplit1=partition(first,last,[&](autox){returncomp(x,pivot);});Itsplit2=partition(split1,last,[&](autox){return!comp(pivot,x);});// Sort left-hand sidequick_sort(first,split1,comp);// Sort right-hand sidequick_sort(split2,last,comp);}}template<classIt>voidquick_sort(Itfirst,Itlast){// If no comparison function is provided, use `std::less`quick_sort(first,last,std::less<std::decay_t<decltype(*first)>>());}