forked from TheAlgorithms/C-Plus-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOddEven Sort.cpp
More file actions
61 lines (51 loc) · 909 Bytes
/
OddEven Sort.cpp
File metadata and controls
61 lines (51 loc) · 909 Bytes
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
/* 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;
}