(C++) InsertionSort
January 11, 2018 · View on GitHub
(C++) InsertionSort
InsertionSort is a sorting code snippet to perform an insertion sort.
#include <algorithm> #include <vector> ///Sort a std::vector using insertion sort ///From http://www.richelbilderbeek.nl/CppInsertionSort.htm template <typename T> void InsertionSort(std::vector<T>& v) { const int size = v.size(); for(int i=1; i!=size; ++i) { for(int j=0; j<i; ++j) { if (v[j] > v[i]) { const int temp = v[j]; v[j] = v[i]; for(int k=i; k>j; --k) { v[k] = v[k-1]; } v[j+1] = temp; } } } }