Open In App

Sorted Array to Wave Form

Last Updated : 11 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a sorted array of integers, the task is to sort the array into a wave array. An array arr[0..n-1] is sorted in wave form if:
arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= .....

Examples:

Input:  arr[] = {1, 2, 3, 4, 5}
Output: arr[] = {2, 1, 4, 3, 5} 
Explanation: The output array has the form: arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= .....

Input: arr[] = {2, 4, 7, 8, 9, 10}
Output: arr[] = {4, 2, 8, 7, 10, 9}

Approach:

The idea is to utilize the fact that the array is already sorted. By simply swapping adjacent elements starting from index 0, we automatically get a wave pattern since each swapped element from an even index will be greater than its neighbors, while each swapped element from an odd index will be smaller than its neighbors.

Below is the implementation of the above approach:

C++
// C++ program to sort an array in wave form.
#include<bits/stdc++.h>
using namespace std;

// This function sorts arr[0..n-1] in wave form
void convertToWave(vector<int> &arr) {
    int n = arr.size();

    // Swap adjacent elements to create wave pattern
    for (int i = 0; i < n-1; i += 2) {
        swap(arr[i], arr[i + 1]);
    }
}

int main() {
    vector<int> arr = {1, 2, 3, 4, 5};
    convertToWave(arr);
    for (int i=0; i<arr.size(); i++)
       cout << arr[i] << " ";
    return 0;
}
Java
// Java program to sort an array in wave form.
import java.util.*;

class GfG {
  
    // This function sorts arr[0..n-1] in wave form
    static void convertToWave(int[] arr) {
        int n = arr.length;

        // Swap adjacent elements to create wave pattern
        for (int i = 0; i < n - 1; i += 2) {
            int temp = arr[i];
            arr[i] = arr[i + 1];
            arr[i + 1] = temp;
        }
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        convertToWave(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
Python
# Python program to sort an array in wave form.

# This function sorts arr[0..n-1] in wave form
def convertToWave(arr):
    n = len(arr)

    # Swap adjacent elements to create wave pattern
    for i in range(0, n - 1, 2):
        arr[i], arr[i + 1] = arr[i + 1], arr[i]

if __name__ == "__main__":
    arr = [1, 2, 3, 4, 5]
    convertToWave(arr)
    print(" ".join(map(str, arr)))
C#
// C# program to sort an array in wave form.
using System;

class GfG {
  
    // This function sorts arr[0..n-1] in wave form
    static void convertToWave(int[] arr) {
        int n = arr.Length;

        // Swap adjacent elements to create wave pattern
        for (int i = 0; i < n - 1; i += 2) {
            int temp = arr[i];
            arr[i] = arr[i + 1];
            arr[i + 1] = temp;
        }
    }

    static void Main(string[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        convertToWave(arr);
        for (int i = 0; i < arr.Length; i++) {
            Console.Write(arr[i] + " ");
        }
    }
}
JavaScript
// JavaScript program to sort an array in wave form.

// This function sorts arr[0..n-1] in wave form
function convertToWave(arr) {
    const n = arr.length;

    // Swap adjacent elements to create wave pattern
    for (let i = 0; i < n - 1; i += 2) {
        let temp = arr[i];
        arr[i] = arr[i + 1];
        arr[i + 1] = temp;
    }
}

const arr = [1, 2, 3, 4, 5];
convertToWave(arr);
console.log(arr.join(" "));

Output
2 1 4 3 5 

Time Complexity: O(n)
Auxiliary Space: O(1)


Next Article
Article Tags :
Practice Tags :

Similar Reads