forked from Sniper7sumit/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWave_Sort.cpp
More file actions
50 lines (39 loc) · 828 Bytes
/
Wave_Sort.cpp
File metadata and controls
50 lines (39 loc) · 828 Bytes
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Name: JHALA DEVRAJSINH SHRIPALSINH
// Date: 01/07/2021
// Purpose: Wave Sort
// Wave form like arr[0]>= arr[1] <= arr[2] >= arr[3] <= arr[4] >= ...
// Time Complexity:
// O(N/2) === O(N)
// Final time complexity === O(N)
#include<bits/stdc++.h>
using namespace std;
void swap(int arr[], int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
void wavesort(int arr[], int n)
{
for(int i=1; i<n; i+=2)
{
if(arr[i] > arr[i-1])
{
swap(arr,i,i-1);
}
if(arr[i] > arr[i+1] && i <= n-2)
{
swap(arr, i,i+1);
}
}
}
int main()
{
int arr[] = {1,3,4,7,5,6,2};
wavesort(arr,7);
for(int i=0;i<7;i++)
{
cout << arr[i] << " ";
}
return 0;
}