Skip to content
This repository has been archived by the owner on Oct 4, 2022. It is now read-only.

added a wave sort array C++ program #1292

Closed
wants to merge 1 commit into from
Closed
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
38 changes: 38 additions & 0 deletions C++/Algorithms/Sorting/waveSortArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Code In C++ Lnaguage
#include<bits/stdc++.h>
using namespace std;

// created a class WaveSort
class WaveSort{
public:
// class method convertToWaveForm
void convertToWaveForm(int *arr, int n){
// swapping the positions of arr[i] & arr[i+1] by incrementing loop after 2 steps
for(int i=0; i<n-1; i+=2)
{
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
};

int main()
{
int n=7;
int a[n]={100, 2, 1, 6, 19, 27, 5};

// sorting the entire array before performing wave sort
sort(a,a+n);

// created an object of class WaveSort and calling its method convertToWaveForm to perform wave sort on the sorted array
WaveSort ob;
ob.convertToWaveForm(a, n);

// displaying the final array
cout<<"Array after performing wave sort would be -->"<<endl;
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}