-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
odd_even_sort.cpp
53 lines (43 loc) · 1.06 KB
/
odd_even_sort.cpp
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
/* C++ implementation Odd Even Sort */
#include <iostream>
#include <vector>
using namespace std;
void oddEven(vector<int> &arr, int size) {
bool sorted = false;
while (!sorted) {
sorted = true;
for (int i = 1; i < size - 1; i += 2) // Odd
{
if (arr[i] > arr[i + 1]) {
swap(arr[i], arr[i + 1]);
sorted = false;
}
}
for (int i = 0; i < size - 1; i += 2) // Even
{
if (arr[i] > arr[i + 1]) {
swap(arr[i], arr[i + 1]);
sorted = false;
}
}
}
}
void show(vector<int> A, int size) {
int i;
for (i = 0; i < size; i++) cout << A[i] << "\n";
}
int main() {
int size, temp;
cout << "\nEnter the number of elements : ";
cin >> size;
vector<int> arr;
cout << "\nEnter the unsorted elements : \n";
for (int i = 0; i < size; ++i) {
cin >> temp;
arr.push_back(temp);
}
oddEven(arr, size);
cout << "Sorted array\n";
show(arr, size);
return 0;
}