GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — C-Plus-Plus/sorting/cocktail_selection_sort.cpp at master · https-hari/C-Plus-Plus

Expand file tree

/

cocktail_selection_sort.cpp

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
102 lines (87 loc) · 2.69 KB
/

cocktail_selection_sort.cpp

Copy path

File metadata and controls

102 lines (87 loc) · 2.69 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Returns Sorted elements after performing Cocktail Selection Sort
// It is a Sorting algorithm which chooses the minimum and maximum element in an
// array simultaneously, and swaps it with the lowest and highest available
// position iteratively or recursively
#include <algorithm>
#include <iostream>
#include <vector>
// Iterative Version
void CocktailSelectionSort(std::vector<int> *vec, int low, int high) {
while (low <= high) {
int minimum = (*vec)[low];
int minimumindex = low;
int maximum = (*vec)[high];
int maximumindex = high;
for (int i = low; i <= high; i++) {
if ((*vec)[i] >= maximum) {
maximum = (*vec)[i];
maximumindex = i;
}
if ((*vec)[i] <= minimum) {
minimum = (*vec)[i];
minimumindex = i;
}
}
if (low != maximumindex || high != minimumindex) {
std::swap((*vec)[low], (*vec)[minimumindex]);
std::swap((*vec)[high], (*vec)[maximumindex]);
} else {
std::swap((*vec)[low], (*vec)[high]);
}
low++;
high--;
}
}
// Recursive Version
void CocktailSelectionSort_v2(std::vector<int> *vec, int low, int high) {
if (low >= high)
return;
int minimum = (*vec)[low];
int minimumindex = low;
int maximum = (*vec)[high];
int maximumindex = high;
for (int i = low; i <= high; i++) {
if ((*vec)[i] >= maximum) {
maximum = (*vec)[i];
maximumindex = i;
}
if ((*vec)[i] <= minimum) {
minimum = (*vec)[i];
minimumindex = i;
}
}
if (low != maximumindex || high != minimumindex) {
std::swap((*vec)[low], (*vec)[minimumindex]);
std::swap((*vec)[high], (*vec)[maximumindex]);
} else {
std::swap((*vec)[low], (*vec)[high]);
}
CocktailSelectionSort(vec, low + 1, high - 1);
}
// main function, select any one of iterative or recursive version
int main() {
int n;
std::cout << "Enter number of elements\n";
std::cin >> n;
std::vector<int> v(n);
std::cout << "Enter all the elements\n";
for (int i = 0; i < n; ++i) {
std::cin >> v[i];
}
int method;
std::cout << "Enter method: \n\t0: iterative\n\t1: recursive:\t";
std::cin >> method;
if (method == 0) {
CocktailSelectionSort(&v, 0, n - 1);
} else if (method == 1) {
CocktailSelectionSort_v2(&v, 0, n - 1);
} else {
std::cerr << "Unknown method" << std::endl;
return -1;
}
std::cout << "Sorted elements are\n";
for (int i = 0; i < n; ++i) {
std::cout << v[i] << " ";
}
return 0;
}
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.