Skip to content

Commit

Permalink
Added code for odd-even sort
Browse files Browse the repository at this point in the history
  • Loading branch information
nishchay2517 committed Oct 25, 2023
1 parent 3e44048 commit 8413a39
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Computational_Algorithms/Sorting_Algorithms/odd_even_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include<bits/stdc++.h>
using namespace std;
void odd_even_sort(vector<int> &arr , int n){
bool is_sorted = false;

while (!is_sorted)
{
is_sorted = true;
for (int i = 0; i <= n-2; i = i+2)
{
if(arr[i] >arr[i+1]){
swap(arr[i],arr[i+1]);
is_sorted = false;
}
}
for (int i = 1; i <= n-2; i = i+2)
{
if(arr[i] >arr[i+1]){
swap(arr[i],arr[i+1]);
is_sorted = false;
}
}
}
}
int main()
{
vector<int> arr ={5 ,2 ,6 ,78 ,8};
odd_even_sort(arr , arr.size());
for(auto ele : arr){
cout<<ele<<endl;
}
return 0;
}

0 comments on commit 8413a39

Please sign in to comment.