chore(CPlusPlus): added wave-sort (#382)
Co-authored-by: Arsenic <54987647+Arsenic-ATG@users.noreply.github.com>pull/390/head
parent
8a2bd30a39
commit
1d0e8322ef
|
@ -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
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue