#280
Medium Algorithms Wiggle sort
Array Greedy Sorting
68.4% acceptance
Mar 31, 2026
1247
103
Given an integer array nums, reorder it such that nums[0] <= nums[1] >= nums[2] <= nums[3]....
You may assume the input array always has a valid answer.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn wiggle_sort(nums: &mut Vec<i32>) {
for i in 1..nums.len() {
if (i % 2 == 1 && nums[i] < nums[i - 1]) || (i % 2 == 0 && nums[i] > nums[i - 1]) {
nums.swap(i, i - 1);
}
}
}
}