12void CocktailSelectionSort(std::vector<int> *vec,
int low,
int high) {
14 int minimum = (*vec)[low];
15 int minimumindex = low;
16 int maximum = (*vec)[high];
17 int maximumindex = high;
19 for (
int i = low; i <= high; i++) {
20 if ((*vec)[i] >= maximum) {
24 if ((*vec)[i] <= minimum) {
29 if (low != maximumindex || high != minimumindex) {
30 std::swap((*vec)[low], (*vec)[minimumindex]);
31 std::swap((*vec)[high], (*vec)[maximumindex]);
33 std::swap((*vec)[low], (*vec)[high]);
43void CocktailSelectionSort_v2(std::vector<int> *vec,
int low,
int high) {
47 int minimum = (*vec)[low];
48 int minimumindex = low;
49 int maximum = (*vec)[high];
50 int maximumindex = high;
52 for (
int i = low; i <= high; i++) {
53 if ((*vec)[i] >= maximum) {
57 if ((*vec)[i] <= minimum) {
62 if (low != maximumindex || high != minimumindex) {
63 std::swap((*vec)[low], (*vec)[minimumindex]);
64 std::swap((*vec)[high], (*vec)[maximumindex]);
66 std::swap((*vec)[low], (*vec)[high]);
69 CocktailSelectionSort(vec, low + 1, high - 1);
76 std::cout <<
"Enter number of elements\n";
78 std::vector<int> v(n);
79 std::cout <<
"Enter all the elements\n";
80 for (
int i = 0; i < n; ++i) {
85 std::cout <<
"Enter method: \n\t0: iterative\n\t1: recursive:\t";
89 CocktailSelectionSort(&v, 0, n - 1);
90 }
else if (method == 1) {
91 CocktailSelectionSort_v2(&v, 0, n - 1);
93 std::cerr <<
"Unknown method" << std::endl;
96 std::cout <<
"Sorted elements are\n";
97 for (
int i = 0; i < n; ++i) {
98 std::cout << v[i] <<
" ";