forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wave_sort.cpp
69 lines (59 loc) · 1.52 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* Problem statement :
Given an unsorted array. Sort the array in such a way that the array looks like a wave array.
For example, if the given sequence arr has n elements then the sorted wave array looks like -
arr[0] >= arr[1] and arr[1] <= arr[2]
arr[2] >= arr[3] and arr[3] <= arr[4]
arr[4] >= arr[5] and arr[5] <= arr[6] And so on
For example, the given array : arr = { 4, 3, 5, 2, 3, 1, 2 }
The above figure is a visual representation of the given arr and you can see we can express ‘arr’ in a waveform array because
4>3 and 3<5
5>2 and 2<3
3>1 and 1<2
And it follows the condition of wave array */
#include <bits/stdc++.h>
using namespace std;
vector<int> wave_sort(vector<int> &v, int n)
{
for (int i = 0; i < n; i += 2)
{
/*If the current element is lesser in value than the previous and is not the first.
then the values are swapped */
if (v[i] < v[i - 1] and i != 0)
{
swap(v[i], v[i - 1]);
}
/*If the current element is lesser in value than the next and is not the last.
then the values are swapped */
if (v[i] < v[i + 1] and i != n - 1)
{
swap(v[i], v[i + 1]);
}
}
return v;
}
int main()
{
int n;
vector<int> v;
cin >> n;
for (int i = 0; i < n; i++)
{
int temp;
cin >> temp;
v.push_back(temp);
}
wave_sort(v, n);
//Printing the final vector
for (int i = 0; i < n; i++)
{
cout << v[i] << " ";
}
return 0;
}
/* Example 1:
Input[] : {20 80 40 35 10 15 70}
Output[] : {80 20 40 10 35 15 70}
Example 2:
Input[] = {12 20 45 60 5}
Output[] : {20 12 60 5 45}
Time Complexity: O(n) */