-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
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
Closed
Closed
Update odd_even_sort.cpp #2737
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. */ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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