From 9361eeb0b9626a8656533adf43f537b1ef192223 Mon Sep 17 00:00:00 2001 From: Khushboo Shakya <72929479+khushbooshakya@users.noreply.github.com> Date: Tue, 24 Sep 2024 12:46:32 +0530 Subject: [PATCH] Update odd_even_sort.cpp Updated the odd even sort code with comments and simplified it to its most basic form. --- sorting/odd_even_sort.cpp | 63 +++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/sorting/odd_even_sort.cpp b/sorting/odd_even_sort.cpp index 25f2a371228..cdb91a3677c 100644 --- a/sorting/odd_even_sort.cpp +++ b/sorting/odd_even_sort.cpp @@ -1,53 +1,66 @@ -/* C++ implementation Odd Even Sort */ #include #include - using namespace std; -void oddEven(vector &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 &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 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 &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 arr; - - cout << "\nEnter the unsorted elements : \n"; + vector 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. */