From 1d0e8322eff745d62c49ee78eba8597232bb3a5f Mon Sep 17 00:00:00 2001 From: Janhavi Pimplikar <76153468+Janhavi-2001@users.noreply.github.com> Date: Mon, 12 Jul 2021 17:59:45 +0530 Subject: [PATCH] chore(CPlusPlus): added wave-sort (#382) Co-authored-by: Arsenic <54987647+Arsenic-ATG@users.noreply.github.com> --- algorithms/CPlusPlus/README.md | 1 + algorithms/CPlusPlus/Sorting/wave-sort.cpp | 44 ++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 algorithms/CPlusPlus/Sorting/wave-sort.cpp diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 3ec34a3c..c76ea6ea 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -68,6 +68,7 @@ 11. [Shell Sort](Sorting/shell-sort.cpp) 12. [Binary Insertion Sort](Sorting/binary-insertion-sort.cpp) 13. [Merge Sort](Sorting/merge-sort.cpp) +14. [Wave Sort](Sorting/wave-sort.cpp) ## Strings diff --git a/algorithms/CPlusPlus/Sorting/wave-sort.cpp b/algorithms/CPlusPlus/Sorting/wave-sort.cpp new file mode 100644 index 00000000..b8456f56 --- /dev/null +++ b/algorithms/CPlusPlus/Sorting/wave-sort.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; + +void WaveSort(int arr[], int n) +{ + for(int i=1;i arr[i+1] && i<=n-2) + { + int temp=arr[i]; + arr[i+1]=arr[i]; + arr[i]=temp; + } + if(arr[i] > arr[i-1]) + { + int temp=arr[i]; + arr[i]=arr[i-1]; + arr[i-1]=temp; + + } + } +} + +int main() +{ + int arr[] = {1,2,3,4,5,6,7,8}; + cout << "The array before sorting: " << endl; + for(int i = 0; i < 8; i++) + { + cout << arr[i] << " "; + } + cout << endl; + WaveSort(arr, 8); + cout << "The array after sorting: " << endl; + for(int i = 0; i < 8; i++) + { + cout << arr[i] << " "; + } + return 0; + + //Expected time complexity : O(nlogn); + //Expected space complexity: O(1); +} +