-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbrick_sort.cpp
More file actions
48 lines (40 loc) · 1.09 KB
/
Copy pathbrick_sort.cpp
File metadata and controls
48 lines (40 loc) · 1.09 KB
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
//#include<bits/stdc++.h>
#include <iostream>
using namespace std;
// A utility function ot print an array of size n
void printArray(int arr[], int n) {
for (int i=0; i < n; i++)
cout << arr[i] << " ";
cout << "\n";
}
// A function to sort the algorithm using Odd Even sort
void oddEvenSort(int arr[], int n)
{
bool isSorted = false; // Initially array is unsorted
while (!isSorted) {
isSorted = true;
// Perform Bubble sort on odd indexed element
for (int i=1; i<=n-2; i=i+2) {
if (arr[i] > arr[i+1]) {
swap(arr[i], arr[i+1]);
isSorted = false;
}
}
// Perform Bubble sort on even indexed element
for (int i=0; i<=n-2; i=i+2) {
if (arr[i] > arr[i+1]) {
swap(arr[i], arr[i+1]);
isSorted = false;
}
}
}
return;
}
// Driver program to test above functions.
int main() {
int arr[] = {3,2,3,8,5,6,4,1};
int n = sizeof(arr)/sizeof(arr[0]);
oddEvenSort(arr, n);
printArray(arr, n);
return (0);
}