Sorted Array to Wave Form (original) (raw)
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 &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 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
- Sort a Rotated Sorted Array You are given a rotated sorted array and your aim is to restore its original sort in place.Expected to use O(1) extra space and O(n) time complexity. Examples: Input : [3, 4, 1, 2] Output : [1, 2, 3, 4] Input : [2, 3, 4, 1] Output : [1, 2, 3, 4] We find the point of rotation. Then we rotate array us 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
- Create a wave array from the given Binary Search Tree Given a Binary Search Tree, the task is to create a wave array from the given Binary Search Tree. An array arr[0..n-1] is called a wave array if arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= ... Examples: Input: Output: 4 2 8 6 12 10 14Explanation: The above mentioned array {4, 2, 7 min read
- How to Sort a Multi-dimensional Array by Value What is Sorting?Arranging data in an increasing or decreasing fashion according to their values is called Sorting. Below are shown some processes for sorting arrays of several dimensions. Sorting a 1-Dimensional array:We can sort any Dimensional array using the sort method in C++. Syntax: sort(arr, 11 min read
- Convert Array into Zig-Zag fashion Given an array of distinct elements of size N, the task is to rearrange the elements of the array in a zig-zag fashion, i.e., the elements are arranged as smaller, then larger, then smaller, and so on. There can be more than one arrangement that follows the form: arr[0] < arr[1] > arr[2] < 12 min read
- Sort an array having 3 types of Values We are given an array containing three different types of elements, and the task is to sort the array. This problem has been asked in various forms, and in this article, we will explore the three most common forms of this problem.Table of ContentSort an array of 0s, 1s and 2sThree way partitioning a 4 min read