chore(CPlusPlus): added wave-sort (#382)

Co-authored-by: Arsenic <54987647+Arsenic-ATG@users.noreply.github.com>
pull/390/head
Janhavi Pimplikar 2021-07-12 17:59:45 +05:30 committed by GitHub
parent 8a2bd30a39
commit 1d0e8322ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 0 deletions

View File

@ -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

View File

@ -0,0 +1,44 @@
#include <iostream>
using namespace std;
void WaveSort(int arr[], int n)
{
for(int i=1;i<n;i+=2)
{
if(arr[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);
}