Javascript Program to Sort an array in wave form
Last Updated :
17 Sep, 2024
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
{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.
JavaScript
// A JavaScript program to sort an array
// in wave form using a sorting function
// A utility method to swap two numbers.
function swap(arr, x, y) {
let temp = arr[x];
arr[x] = arr[y];
arr[y] = temp
}
// This function sorts arr[0..n-1] in
// wave form, i.e.,
// arr[0] >= arr[1] <= arr[2] >=
// arr[3] <= arr[4] >= arr[5]..
function sortInWave(arr, n) {
// Sort the input array
arr.sort((a, b) => a - b);
// Swap adjacent elements
for (let i = 0; i < n - 1; i += 2)
swap(arr, i, i + 1);
}
// Driver code
let arr = [10, 90, 49, 2, 1, 5, 23];
let n = arr.length;
sortInWave(arr, n);
console.log(arr);
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.
- Traverse all even positioned elements of input array, and do following.
- If current element is smaller than previous odd element, swap previous and current.
- If current element is smaller than next odd element, swap next and current.
Below are implementations of above simple algorithm.
JavaScript
// A O(n) program to sort
// an input array in wave form
// A utility method to swap two numbers.
function swap(arr, a, b) {
let temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
// This function sorts arr[0..n-1]
// in wave form, i.e.,
// arr[0] >= arr[1] <= arr[2] >=
// arr[3] <= arr[4]....
function sortInWave(arr, n) {
// Traverse all even elements
for (let 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 - 1, i);
// If current even element
// is smaller than next
if (i < n - 1 && arr[i] < arr[i + 1])
swap(arr, i, i + 1);
}
}
// driver code
let arr = [10, 90, 49, 2, 1, 5, 23];
let n = arr.length;
sortInWave(arr, n);
console.log(arr);
Output[
90, 10, 49, 1,
5, 2, 23
]
Please refer complete article on Sort an array in wave form for more details!
Similar Reads
Sort An Array Of Arrays In JavaScript
The following approaches can be used to sort array of arrays in JavaScript.1. Using array.sort() Method- Mostly UsedJS array.sort() is used to sort and update the original array in ascending order.JavaScriptlet arr = [[3, 2], [1, 4], [2, 5], [2, 0]]; arr.sort(); console.log(arr);Output[ [ 1, 4 ], [
2 min read
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 For Sorting An Array Of 0s, 1s and 2s
Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last.Examples:Input: {0, 1, 2, 0, 1, 2}Output: {0, 0, 1, 1, 2, 2}Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1}Output: {0, 0, 0, 0, 0
5 min read
How to sort an Array of Strings in Java
Array Of StringsTo sort an array of strings in Java, we can use Arrays.sort() function. Java // A sample Java program to // sort an array of strings // in ascending and descending // orders using Arrays.sort(). import java.util.Arrays; import java.util.Collections; // Driver Class public class SortE
3 min read
Sorted Array to Wave Form
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
3 min read
JavaScript Program to find the Nth smallest/largest element from an unsorted Array
In this article, we will see how to find Nth largest and smallest element from an unsorted array. The Nth smallest/largest element from an unsorted array refers to the element that ranks at the Nth position when the array is sorted either in ascending (smallest) or descending (largest) order. It rep
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
Program to Print Mirror Image of Sine-Wave Pattern
A sine wave is a mathematical curve that oscillates between a maximum and minimum value, smoothly transitioning between positive and negative values. It is commonly used to represent periodic phenomena such as sound, light, and electrical signals.Examples: Give the Height and Width of a Wave to prin
5 min read
Javascript Program For Converting Array Into Zig-Zag Fashion
Given an array of DISTINCT elements, rearrange the elements of array in zig-zag fashion in O(n) time. The converted array should be in form a < b > c < d > e < f. Example:Input: arr[] = {4, 3, 7, 8, 6, 2, 1} Output: arr[] = {3, 7, 4, 8, 2, 6, 1}Input: arr[] = {1, 4, 3, 2} Output: arr[
3 min read
Javascript Program For Rearranging An Array In Maximum Minimum Form - Set 2 (O(1) extra space)
Given a sorted array of positive integers, rearrange the array alternately i.e first element should be the maximum value, second minimum value, third-second max, fourth-second min and so on. Examples:Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: arr[] = {7, 1, 6, 2, 5, 3, 4}Input: arr[] = {1, 2, 3, 4
5 min read