[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppSelectionSort
Incomplete list of Sorting functions:
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppSelectionSort
(C++) Selection sort
Sorting function.Incomplete list of Sorting functions:
template <typename T> void SelectionSort(std::vector<T>& v) { const int size = v.size(); for(int i=0; i!=size-1; ++i) { for(int j=i+1; j!=size; ++j) { if (v[i]> v[j]) { std::swap(v[i],v[j]); } } } } int main() { //Create a std::vector<int> with random values std::vector<int> v; for (int i=0; i<100; ++i) v.push_back(std::rand() % 1000); //Create a copy of this std::vector std::vector<int> v1(v); //Sort v using std::sort std::sort(v.begin(),v.end()); //Sort v1 using SelectionSort SelectionSort(v1); //Assert they are equal assert(v1==v); }
- include <vector>
- include <algorithm>
- include <cassert>
'Selection Sort' links
- general (in Computer Science)
Code links
- assert
- cassert (header file)
- const
- for
- if
- int
- iostream (header file)
- main
- std
- std::sort
- template
- typename
- std::vector
- vector
- vector (header file)
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
