diff options
Diffstat (limited to 'straight-selection.cpp')
| -rw-r--r-- | straight-selection.cpp | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/straight-selection.cpp b/straight-selection.cpp new file mode 100644 index 0000000..ff5a7b6 --- /dev/null +++ b/straight-selection.cpp @@ -0,0 +1,47 @@ +#include <iostream> +#include <vector> +#include <ostream> + +template<class T> void +straight_selection(std::vector<T> &array) +{ + for (int i = 0; i < array.size(); ++i) + { + int k = i; + int x = array[i]; + for (int j = i; j < array.size(); ++j) + { + if (array[j] < x) + { + k = j; + x = array[j]; + } + } + array[k] = array[i]; + array[i] = x; + } +} + +template<class T> std::ostream & +operator<<(std::ostream &stream, std::vector<T> &v) +{ + if (v.size() == 0) stream << "{ }"; + + stream << "array = { "; + for (int i = 0; i < int(v.size()) - 1; ++i) + stream << v[i] << ", "; + + stream << v.back() << " }"; + return stream; +} + +int +main() +{ + std::vector<int> array = { 2, 7, 12, 30, 11, 4, 12, 5, 20 }; + std::cout << array << std::endl; + straight_selection(array); + std::cout << array << std::endl; + + return 0; +} |