Skip to content

Commit 3742efd

Browse files
author
hasibulislam999
committed
Wiggle Sort II problem solved
1 parent da698c1 commit 3742efd

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Quickselect/324_wiggle-sort-ii.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Title: Wiggle Sort II
3+
* Description: Given an integer array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]....
4+
* Author: Hasibul Islam
5+
* Date: 03/04/2023
6+
*/
7+
8+
/**
9+
* @param {number[]} nums
10+
* @return {void} Do not return anything, modify nums in-place instead.
11+
*/
12+
var wiggleSort = function (nums) {
13+
const n = nums.length;
14+
const arr = [...nums];
15+
arr.sort((a, b) => a - b);
16+
let i = 0;
17+
let j = (n - 1) >> 1;
18+
let k = n - 1;
19+
while (i < n) {
20+
nums[i] = arr[j--];
21+
if (i + 1 < n) {
22+
nums[i + 1] = arr[k--];
23+
}
24+
i += 2;
25+
}
26+
return nums;
27+
};
28+
29+
console.log(wiggleSort([1, 5, 1, 1, 6, 4]));

0 commit comments

Comments
 (0)