TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
odd_even_sort.cpp
1/* C++ implementation Odd Even Sort */
2#include <iostream>
3#include <vector>
4
5using namespace std;
6
7void oddEven(vector<int> &arr, int size) {
8 bool sorted = false;
9 while (!sorted) {
10 sorted = true;
11 for (int i = 1; i < size - 1; i += 2) // Odd
12 {
13 if (arr[i] > arr[i + 1]) {
14 swap(arr[i], arr[i + 1]);
15 sorted = false;
16 }
17 }
18
19 for (int i = 0; i < size - 1; i += 2) // Even
20 {
21 if (arr[i] > arr[i + 1]) {
22 swap(arr[i], arr[i + 1]);
23 sorted = false;
24 }
25 }
26 }
27}
28
29void show(vector<int> A, int size) {
30 int i;
31 for (i = 0; i < size; i++) cout << A[i] << "\n";
32}
33
34int main() {
35 int size, temp;
36 cout << "\nEnter the number of elements : ";
37 cin >> size;
38
39 vector<int> arr;
40
41 cout << "\nEnter the unsorted elements : \n";
42
43 for (int i = 0; i < size; ++i) {
44 cin >> temp;
45 arr.push_back(temp);
46 }
47
48 oddEven(arr, size);
49
50 cout << "Sorted array\n";
51 show(arr, size);
52 return 0;
53}
int main()
Main function.