Skip to content

Commit

Permalink
"added comment"
Browse files Browse the repository at this point in the history
  • Loading branch information
nishchay2517 committed Oct 27, 2023
1 parent 8413a39 commit 5ce3b06
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions Computational_Algorithms/Sorting_Algorithms/odd_even_sort.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#include<bits/stdc++.h>
using namespace std;
void odd_even_sort(vector<int> &arr , int n){
bool is_sorted = false;

bool is_sorted = false; //initialised a flag to track if array is sorted or not.
while (!is_sorted)
{
is_sorted = true;
for (int i = 0; i <= n-2; i = i+2)
is_sorted = true; //make flag as sorted initially
for (int i = 0; i <= n-2; i = i+2) //loop over even elements
{
if(arr[i] >arr[i+1]){
swap(arr[i],arr[i+1]);
is_sorted = false;
swap(arr[i],arr[i+1]); //if next element is smaller than swap the elements
is_sorted = false;//mark the flag as unsorted as we have swapped the elements
}
}
for (int i = 1; i <= n-2; i = i+2)
for (int i = 1; i <= n-2; i = i+2) //loops over odd elements
{
if(arr[i] >arr[i+1]){
swap(arr[i],arr[i+1]);
is_sorted = false;
swap(arr[i],arr[i+1]);//if next element is smaller than swap the elements
is_sorted = false; //mark the flag as unsorted as we have swapped the elements
}
}
}
Expand Down

0 comments on commit 5ce3b06

Please sign in to comment.