-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortInWaveForm.cpp
More file actions
39 lines (29 loc) · 726 Bytes
/
SortInWaveForm.cpp
File metadata and controls
39 lines (29 loc) · 726 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
#include<iostream>
#include<algorithm>
using namespace std;
void swap(int * p, int * q){
int temp = * p;
* p = * q;
* q = temp;
}
void array_in_wave(int array[], int n){
sort(array, array + n);
for (int i = 0; i < n - 1; i += 2)
swap( & array[i], & array[i + 1]);
}
int main(){
int array[100], n, i;
cout << "Enter number of elements: ";
cin >> n;
cout << "\nEnter elements: ";
for (i = 0; i < n; i++)
cin >> array[i];
cout << "Original array: ";
for (int i = 0; i < n; i++)
cout << array[i] << " ";
array_in_wave(array, n);
cout << "\nWave form of the array is: ";
for (int i = 0; i < n; i++)
cout << array[i] << " ";
return 0;
}