Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Uneven sorting of array in JavaScript
We need to write a JavaScript function that sorts an array in a zigzag pattern where elements alternate between smaller and larger values: arr[0] < arr[1] > arr[2] < arr[3]...
Problem Statement
Given an array of numbers, we want to rearrange it so that:
- Even indices (0, 2, 4...) have smaller values
- Odd indices (1, 3, 5...) have larger values
- The pattern creates a "wave" or zigzag effect
For example, if the input array is [1, 5, 1, 1, 6, 4], a valid output could be [1, 6, 1, 5, 1, 4].
Solution Approach
The algorithm works by:
- Sorting the array in ascending order
- Splitting it into two halves (smaller and larger elements)
- Placing smaller elements at even indices and larger elements at odd indices
Implementation
const arr = [1, 5, 1, 1, 6, 4];
const unevenSort = (arr = []) => {
// Sort array in ascending order
arr.sort((a, b) => a - b);
// Calculate midpoint, adjusting for odd length
let mid = Math.floor(arr.length / 2);
if (arr.length % 2 === 1) {
mid += 1;
}
// Split into smaller and larger halves
let smaller = arr.slice(0, mid);
let larger = arr.slice(mid);
// Fill array with zigzag pattern
for (let i = 0; i < arr.length; i++) {
if (i % 2 === 0) {
// Even indices get smaller values (from end)
arr[i] = smaller.pop();
} else {
// Odd indices get larger values (from end)
arr[i] = larger.pop();
}
}
};
unevenSort(arr);
console.log(arr);
[1, 6, 1, 5, 1, 4]
How It Works
Let's trace through the algorithm step by step:
const testArray = [1, 5, 1, 1, 6, 4];
console.log("Original:", testArray);
// Step 1: Sort
testArray.sort((a, b) => a - b);
console.log("Sorted:", testArray);
// Step 2: Split into halves
let mid = Math.floor(testArray.length / 2) + (testArray.length % 2);
let smaller = testArray.slice(0, mid);
let larger = testArray.slice(mid);
console.log("Smaller half:", smaller);
console.log("Larger half:", larger);
Original: [1, 5, 1, 1, 6, 4] Sorted: [1, 1, 1, 4, 5, 6] Smaller half: [1, 1, 1] Larger half: [4, 5, 6]
Alternative Implementation
Here's a non-mutating version that returns a new array:
const createZigzagArray = (inputArr) => {
const sorted = [...inputArr].sort((a, b) => a - b);
const result = [];
let mid = Math.floor(sorted.length / 2);
if (sorted.length % 2 === 1) mid += 1;
const smaller = sorted.slice(0, mid);
const larger = sorted.slice(mid);
for (let i = 0; i < sorted.length; i++) {
if (i % 2 === 0) {
result[i] = smaller.pop();
} else {
result[i] = larger.pop();
}
}
return result;
};
const original = [3, 7, 2, 1, 8, 5];
const zigzag = createZigzagArray(original);
console.log("Original:", original);
console.log("Zigzag:", zigzag);
Original: [3, 7, 2, 1, 8, 5] Zigzag: [2, 8, 1, 7, 1, 5]
Conclusion
The uneven sorting algorithm creates a zigzag pattern by strategically placing sorted elements at alternating indices. This technique is useful for creating wave-like data patterns and demonstrates effective array manipulation in JavaScript.
Advertisements
