 GeeksforGeeks App
Open App Browser
Continue

# C++ Program to Sort an array in wave form

Given an unsorted array of integers, sort the array into a wave like array. An array ‘arr[0..n-1]’ is sorted in wave form if arr >= arr <= arr >= arr <= arr >= …..

Examples:

``` Input:  arr[] = {10, 5, 6, 3, 2, 20, 100, 80}
Output: arr[] = {10, 5, 6, 2, 20, 3, 100, 80} OR
{20, 5, 10, 2, 80, 6, 100, 3} OR
any other array that is in wave form

Input:  arr[] = {20, 10, 8, 6, 4, 2}
Output: arr[] = {20, 8, 10, 4, 6, 2} OR
{10, 8, 20, 2, 6, 4} OR
any other array that is in wave form

Input:  arr[] = {2, 4, 6, 8, 10, 20}
Output: arr[] = {4, 2, 8, 6, 20, 10} OR
any other array that is in wave form

Input:  arr[] = {3, 6, 5, 10, 7, 20}
Output: arr[] = {6, 3, 10, 5, 20, 7} OR
any other array that is in wave form
```

A Simple Solution is to use sorting. First sort the input array, then swap all adjacent elements.
For example, let 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}.

Below are implementations of this simple approach.

## C++

 `// A C++ program to sort an array in wave form using``// a sorting function``#include``#include``using` `namespace` `std;` `// A utility method to swap two numbers.``void` `swap(``int` `*x, ``int` `*y)``{``    ``int` `temp = *x;``    ``*x = *y;``    ``*y = temp;``}` `// This function sorts arr[0..n-1] in wave form, i.e.,``// arr >= arr <= arr >= arr <= arr >= arr..``void` `sortInWave(``int` `arr[], ``int` `n)``{``    ``// Sort the input array``    ``sort(arr, arr+n);` `    ``// Swap adjacent elements``    ``for` `(``int` `i=0; i

Output:

`2 1 10 5 49 23 90`

The time complexity of the above solution is O(nLogn) if a O(nLogn) sorting algorithm like Merge Sort, Heap Sort, .. etc is used.
This can be done in O(n) time by doing a single traversal of given array.

Space Complexity: O(1) as no extra space has been used.

The idea is based on the fact that if we make sure that all even positioned (at index 0, 2, 4, ..) elements are greater than their adjacent odd elements, we don’t need to worry about odd positioned element. Following are simple steps.
1) Traverse all even positioned elements of input array, and do following.
….a) If current element is smaller than previous odd element, swap previous and current.
….b) If current element is smaller than next odd element, swap next and current.

Below are implementations of above simple algorithm.

## C++

 `// A O(n) program to sort an input array in wave form``#include``using` `namespace` `std;` `// A utility method to swap two numbers.``void` `swap(``int` `*x, ``int` `*y)``{``    ``int` `temp = *x;``    ``*x = *y;``    ``*y = temp;``}` `// This function sorts arr[0..n-1] in wave form, i.e., arr >=``// arr <= arr >= arr <= arr >= arr ....``void` `sortInWave(``int` `arr[], ``int` `n)``{``    ``// Traverse all even elements``    ``for` `(``int` `i = 0; i < n; i+=2)``    ``{``        ``// If current even element is smaller than previous``        ``if` `(i>0 && arr[i-1] > arr[i] )``            ``swap(&arr[i], &arr[i-1]);` `        ``// If current even element is smaller than next``        ``if` `(i

Output:

```90 10 49 1 5 2 23
```

Time Complexity: O(n), where n is the size of the input array. The loop that iterates over even elements in the array runs n/2 times, and each iteration takes constant time.

Space Complexity: O(1), because the algorithm sorts the input array in-place, without using any additional data structures that depend on the input size. The only extra space used is a constant amount of space to store temporary variables during the swap operations.

Please refer complete article on Sort an array in wave form for more details!

My Personal Notes arrow_drop_up