# Sort Array in Waveform in C

## Sort Array in Waveform in C Programming Language

This page is all about converting an array into a waveform. To convert an array in waveform we can use two concepts.

• Brute Force Solution — Using Sorting
• Incrementing the loop by two

we will be learning about both of them in this page, their working and C program. ### Problem Statement

Write a program in C programming language where you need to sort the array in the waveform. There might be multiple possible output of the program. So, you have to print one by using any of the method.

Input Format
-First line contains the value of n that is the total number of elements in the array.
-Second line contains the space separated elements of array.

Output Format
-Output contains only line that is only the resultant array in the wave form fashion.

Sample Test Case : 1

Input
8
10 5 6 3 2 20 98 80

Output
10 5 6 2 20 3 98 80
or
20 5 10 2 80 6 98 3

` `

Sample Test Case : 2

Input
6
20 10 8 6 4 2

Output
20 8 10 4 6 2
or
10 8 20 2 6 4 ### Method 1 : Brute Force Solution (using sorting)

This is a simple method of solving this question which contains basic 2 steps and they are as follow

Step : 1 – Sort the array in ascending order.
Step : 2 – Swap all adjacent elements of the array

Let us consider the input array be {3, 6, 5, 10, 7, 20}. After sorting, we get {3, 5, 6, 7, 10, 20}. After swapping adjacent elements, we get {5, 3, 7, 6, 20, 10}.

If we plot step 1 (that is sorted array) on graph then it would look something like this. If we plot step 2 (that is swapping adjacent elements) on graph then it would look something like this. ### Method : 1 -Solution in C Programming Language

Run
```#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int array[n];
int temp;
for(int i=0; i<n; i++)
{
scanf("%d",&array[i]);
}
//step 1
for(int i=0; i<n; i++)
{
for(int j=i+1; j<n; j++) { if(array[i]>array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
//step 2
for(int i=0; i<n; i=i+2)
{
temp = array[i];
array[i] = array[i+1];
array[i+1] = temp;
}
//printing result
for(int i=0; i<n; i++)
{
printf("%d ",array[i]);
}
return 0;
}```

### Method 2 : Incrementing the loop by 2

The complexity of method used above is O(nLogn) which is used in the algorithms like heap sort etc. This question can be solved in O(n) complexity using single traverse of array.
Following are the steps which will be used to solve this program in O(n) complexity.

Main step : Traverse all even position elements of array (i.e. at index 0, 2, 4, 6, 8 – – -)
Condition 1 : If current element is smaller than previous odd element, swap previous and current.
Condition 2 : If current element is smaller than next odd element, swap next and current.

### Method: 2 – Solution in C Programming Language

Run
```#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int array[n];
int temp;
for(int i=0; i<n; i++)
{
scanf("%d",&array[i]);
}
for(int i=0; i<n; i=i+2) { if(i > 0 && array[i-1] > array[i])
{
temp = array[i];
array[i] = array[i-1];
array[i-1] = temp;
}
if(i < n-1 && array[i] < array[i+1])
{
temp = array[i];
array[i] = array[i+1];
array[i+1] = temp;
}
}
for(int i=0; i < n; i++)
{
printf("%d ",array[i]);
}
return 0;
}```

## Learn About Arrays in C

Click on the below button to learn about in C Programming Language

Learn Arrays in C

## Learn Data Structures

Click on the below button to study more Data Structure

Learn Data Structures

### One comment on “Sort Array in Waveform in C”

• Praveen

Dear prepinsta, please try to solve this question with odd number of elements 0