From 8413a391db7d3d77643fc9b109e0f3d209e2c117 Mon Sep 17 00:00:00 2001 From: nishchay2517 <114824970+nishchay2517@users.noreply.github.com> Date: Wed, 25 Oct 2023 23:02:15 +0530 Subject: [PATCH] Added code for odd-even sort --- .../Sorting_Algorithms/odd_even_sort.cpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Computational_Algorithms/Sorting_Algorithms/odd_even_sort.cpp diff --git a/Computational_Algorithms/Sorting_Algorithms/odd_even_sort.cpp b/Computational_Algorithms/Sorting_Algorithms/odd_even_sort.cpp new file mode 100644 index 0000000..8c14a27 --- /dev/null +++ b/Computational_Algorithms/Sorting_Algorithms/odd_even_sort.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; +void odd_even_sort(vector &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 arr ={5 ,2 ,6 ,78 ,8}; + odd_even_sort(arr , arr.size()); + for(auto ele : arr){ + cout<