-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathWave_sort.cpp
33 lines (30 loc) · 878 Bytes
/
Wave_sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*Name : Atul Kumar
Github username : atul1510
Repositary name : Algorithms
*/
#include<iostream>
using namespace std;
void swap(int arr[], int a, int b) // A utility method to swap two numbers.
{
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
void waveSort(int arr[], int n)
{
for (int i = 1; i < n; i+=2) // Traverse all even elements
{
if(arr[i]>arr[i-1]) swap(arr, i, i-1); // If current even element is smaller than previous
if(arr[i]>arr[i+1] && i<=n-2) swap(arr, i, i+1); // If current even element is smaller than next
}
}
int main() // Driver program to test above function
{
int arr[5] = {5, 4, 3, 2, 1};
waveSort(arr, 5);
for (int i = 0; i < 5; i++) {
cout<<arr[i]<<" ";
}
cout<<endl;
return 0;
}