Sorted Array to Wave Form
Last Updated :
11 Feb, 2025
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(" "));
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Sort an array in wave form Given an unsorted array of integers, 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] >= .....Input: arr[] = {10, 5, 6, 3, 2, 20, 100, 80}Output: arr[] = {10, 5, 6, 2, 20, 3, 100, 80} Explanation: here you
9 min read
Javascript 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[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= .....Examples: Input: arr[] = {10, 5, 6, 3, 2, 20, 100, 80} Output: arr[] = {10, 5, 6, 2, 20, 3, 100, 80} OR {
4 min read
Program to print Sine-Wave Pattern Given two integers waveHeight and waveLength. The task is to print a sine wave pattern using the character 0. The sine wave rises and falls vertically across the given height and repeats horizontally for the given length. Examples:Input: waveHeight = 5, waveLength = 10Output: 0 0 0 0 0 0 0 0 0 0 0 0
15+ min read
Sort a Linked List in wave form Given an unsorted Linked List of integers. The task is to sort the Linked List into a wave like Line. A Linked List is said to be sorted in Wave Form if the list after sorting is in the form: list[0] >= list[1] <= list[2] >= â¦.. Where list[i] denotes the data at i-th node of the Linked List
11 min read
Check if an array is Wave Array Given an array of N positive integers. The task is to check if the array is sorted in wave form. Examples: Input: arr[] = {1, 2, 3, 4, 5}Output: NO Input: arr[] = {1, 5, 3, 7, 2, 8, 6}Output: YES Recommended: Please try your approach on {IDE} first, before moving on to the solution.Approach: First c
14 min read