-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated the odd even sort code with comments and simplified it to its most basic form.
- Loading branch information
1 parent
9374b00
commit 9361eeb
Showing
1 changed file
with
38 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,66 @@ | ||
/* C++ implementation Odd Even Sort */ | ||
#include <iostream> | ||
#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. */ |