Skip to content

Commit ced8a56

Browse files
authored
Merge pull request #453 from mirajkarsaurabh/mirajkarsaurabh-patch-3-1
SortInWaveForm code added
2 parents 1ec974a + 0579fb4 commit ced8a56

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

SortInWaveForm.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include<iostream>
2+
#include<algorithm>
3+
using namespace std;
4+
5+
void swap(int * p, int * q){
6+
int temp = * p;
7+
* p = * q;
8+
* q = temp;
9+
}
10+
11+
void array_in_wave(int array[], int n){
12+
sort(array, array + n);
13+
for (int i = 0; i < n - 1; i += 2)
14+
swap( & array[i], & array[i + 1]);
15+
}
16+
17+
int main(){
18+
int array[100], n, i;
19+
cout << "Enter number of elements: ";
20+
cin >> n;
21+
cout << "\nEnter elements: ";
22+
23+
for (i = 0; i < n; i++)
24+
cin >> array[i];
25+
26+
cout << "Original array: ";
27+
28+
for (int i = 0; i < n; i++)
29+
cout << array[i] << " ";
30+
31+
array_in_wave(array, n);
32+
33+
cout << "\nWave form of the array is: ";
34+
35+
for (int i = 0; i < n; i++)
36+
cout << array[i] << " ";
37+
38+
return 0;
39+
}

0 commit comments

Comments
 (0)