Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added odd even sort in c++ #233

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions OddEven Sort/c++/OddEvenSort-C++.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <bits/stdc++.h>
using namespace std;

void oddEvenSort(int arr[], int n)
{
bool isSorted = false;

while (!isSorted)
{
isSorted = true;

for (int i = 1; i <= n - 2; i = i + 2)
{
if (arr[i] > arr[i + 1])
{
swap(arr[i], arr[i + 1]);
isSorted = false;
}
}

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;
}

void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << "\n";
}

int main()
{
int arr[] = {34, 2, 10, -9};
int n = sizeof(arr) / sizeof(arr[0]);

oddEvenSort(arr, n);
printArray(arr, n);

return (0);
}
Binary file added OddEven Sort/c++/OddEvenSort-C++.exe
Binary file not shown.
8 changes: 8 additions & 0 deletions OddEven Sort/c++/algorithmOddEvenSort-Cpp.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
The algorithm for odd even sort in c++ is as follows:-
example: the array is {34, 2, 10, -9}
First, we consider array to be unsorted,
Then, we run a while loop which begins with considering array as sorted. It runs until array is not sorted. We intialize a
for loop for the elements at odd indices. If arr[i]>arr[i+1], we swap them. With each swap we set isSorted value to false.
A second for loop is initialized for elemnts at even indices. If arr[i]>arr[i+1], we swap them. With each swap we set isSorted
value to false.
This repeated swapping finally sorts the array,