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

Update odd_even_sort.cpp #2737

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
63 changes: 38 additions & 25 deletions sorting/odd_even_sort.cpp
Original file line number Diff line number Diff line change
@@ -1,53 +1,66 @@
/* C++ implementation Odd Even Sort */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a header comment check other files or the contribution guidelines

#include <iostream>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Headers should be documented

#include <vector>

using namespace std;

void oddEven(vector<int> &arr, int size) {
bool sorted = false;
/**
* @brief Odd-Even Sort function
* @param arr The array to be sorted
* @param size The size of the array
*/
void oddEvenSort(vector<int> &arr, int size) {
bool sorted = false; // Initially, the array is assumed to be unsorted
while (!sorted) {
sorted = true;
for (int i = 1; i < size - 1; i += 2) // Odd
{
sorted = true; // Assume the array is sorted

// Perform Bubble sort on odd indexed elements
for (int i = 1; i < size - 1; i += 2) {
if (arr[i] > arr[i + 1]) {
swap(arr[i], arr[i + 1]);
sorted = false;
swap(arr[i], arr[i + 1]); // Swap if the current element is greater than the next
sorted = false; // If a swap is made, array is not sorted yet
}
}

for (int i = 0; i < size - 1; i += 2) // Even
{
// Perform Bubble sort on even indexed elements
for (int i = 0; i < size - 1; i += 2) {
if (arr[i] > arr[i + 1]) {
swap(arr[i], arr[i + 1]);
sorted = false;
swap(arr[i], arr[i + 1]); // Swap if the current element is greater than the next
sorted = false; // If a swap is made, array is not sorted yet
}
}
}
}

void show(vector<int> A, int size) {
int i;
for (i = 0; i < size; i++) cout << A[i] << "\n";
/**
* @brief Display function to print the array
* @param arr The array to be displayed
* @param size The size of the array
*/
void display(const vector<int> &arr, int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << "\n"; // Print each element of the array
}
}

int main() {
int size, temp;
cout << "\nEnter the number of elements : ";
cout << "Enter the number of elements: ";
cin >> size;

vector<int> arr;

cout << "\nEnter the unsorted elements : \n";
vector<int> arr(size); // Create a vector of input size

cout << "Enter the unsorted elements:\n";
for (int i = 0; i < size; ++i) {
cin >> temp;
arr.push_back(temp);
cin >> arr[i]; // Take input from the user
}

oddEven(arr, size);
oddEvenSort(arr, size); // Call the odd-even sort function

cout << "Sorted array:\n";
display(arr, size); // Display the sorted array

cout << "Sorted array\n";
show(arr, size);
return 0;
}
/*
oddEvenSort: This function implements the Odd-Even Sort algorithm. It sorts the array by first processing the odd indices and then the even indices until no swaps are required.
display: A simple function to print the sorted array.
Main Function: Takes input from the user, calls the sorting function, and displays the sorted result. */