From c02685795d01d5aab1de1504301e01f7a39eac09 Mon Sep 17 00:00:00 2001 From: VIRAJ <124988569+viraj200524@users.noreply.github.com> Date: Sat, 12 Oct 2024 13:47:52 +0530 Subject: [PATCH 1/2] Added cocktail shaker algorithm in c++ --- CocktailShaker.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 CocktailShaker.cpp diff --git a/CocktailShaker.cpp b/CocktailShaker.cpp new file mode 100644 index 00000000..e5dd51ec --- /dev/null +++ b/CocktailShaker.cpp @@ -0,0 +1,55 @@ +#include +#include +using namespace std; + +void cocktailShakerSort(vector& arr) { + bool exchanged = true; + int start = 0; + int end = arr.size() - 1; + + while (exchanged) { + exchanged = false; + + // Traverse from left to right + for (int i = start; i < end; ++i) { + if (arr[i] > arr[i + 1]) { + swap(arr[i], arr[i + 1]); + exchanged = true; + } + } + + // If no elements were swapped, the array is already sorted + if (!exchanged) { + break; + } + + exchanged = false; + + // Move the end point back by one + --end; + + // Traverse from right to left + for (int i = end - 1; i >= start; --i) { + if (arr[i] > arr[i + 1]) { + swap(arr[i], arr[i + 1]); + exchanged = true; + } + } + + // Move the start point forward by one + ++start; + } +} + +int main() { + vector arr = {5, 1, 4, 2, 8, 0, 2, 7}; + + cocktailShakerSort(arr); + + cout << "Sorted array: "; + for (int i : arr) { + cout << i << " "; + } + + return 0; +} From abcf7c71aebb58e5f3dd2794a1af1b7bb6c66bfd Mon Sep 17 00:00:00 2001 From: VIRAJ <124988569+viraj200524@users.noreply.github.com> Date: Sat, 12 Oct 2024 13:50:20 +0530 Subject: [PATCH 2/2] Update and rename CocktailShaker.cpp to src/cpp/CocktailShaker.cpp --- CocktailShaker.cpp => src/cpp/CocktailShaker.cpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename CocktailShaker.cpp => src/cpp/CocktailShaker.cpp (100%) diff --git a/CocktailShaker.cpp b/src/cpp/CocktailShaker.cpp similarity index 100% rename from CocktailShaker.cpp rename to src/cpp/CocktailShaker.cpp