Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Wave Sort
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//int this sorting we not sort in ascending or descending we sort it in wave type
//we take jump of i+2 i.e. jump of 2 as we have to check two consecutive crest or trough of wave
#include<iostream>
using namespace std;
void swap(int arr[],int i,int j){
int temp;
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
void wave(int arr[],int n){
for(int i=1;i<n;i+=2){
if(arr[i]>arr[i-1]){
swap(arr,i,i-1); //creating the upper part
}
if(arr[i]>arr[i+1] && i<n-2){
swap(arr,i,i+1); //creating the lower part
}
}
}
int main(){
int n ;
cout<<"Enter size of array ";
cin>>n;
int arr[n];
cout<<"Enter values of array ";
for(int i=0;i<n;i++){
cin>>arr[i];
}
wave(arr,n);
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
return 0;
//time complexity of wave sort is O(n/2) ~ O(n)
}