### Selection Sort (C++ Way)

Selection Sort using Standard Template Libraries (not C++ 11):

Selection sort works by finding smallest element from the list unsorted list, swap with leftmost element and proceed until all the elements are in order.

Selecting the lowest element requires scanning all n elements (this takes n − 1 comparisons) and then swapping it into the first position. Finding the next lowest element requires scanning the remaining n − 1 elements and so on. Hence the complexity is O(N^2).

The C++ implementation is like below:

#include < iostream >
#include < algorithm >
#include < vector >

template < class ForwardIterator1, class ForwardIterator2 >
void iters_swap (ForwardIterator1 a, ForwardIterator2 b)
{
std::swap (*a, *b);
}

template < typename T >
void selectionSort(T &v)
{
// Selection sort
std::vector::iterator it = v.begin();
while(it != v.end())
{
std::vector::iterator i_Min = std::min_element(it, v.end());
iters_swap(i_Min, it);
++it;
}
}

int main()
{
int arr[] = {34, 20, 99, 10, 23};
std::vector v(std::begin(arr), std::end(arr));

// Before sorting
for (std::vector::const_iterator i = v.begin(); i != v.end(); ++i)
{
std::cout << *i << ' ';
}
std::cout << std::endl;

selectionSort(v);

// After sorting
for (std::vector::const_iterator iSort = v.begin(); iSort != v.end(); ++iSort)
{
std::cout << *iSort << ' ';
}
std::cout << std::endl;
return 0;
}

1. Using std::min_element() to get smallest element from vector.
2. Then swapping with leftmost one as required
3. Continuing till rightmost boundary is reached for vector.