Wiggle Sort
Reorder an array in place so that its values go up, down, up, down, with a single pass of swaps.
Reorder an array of integers numsnums[0] <= nums[1] >= nums[2] <= nums[3] >= ...
Example 1
Output: [3, 5, 1, 6, 2, 4]
Explanation: [1, 6, 2, 5, 3, 4] and [2, 6, 1, 5, 3, 4] are also valid.
Example 2
Output: [1, 3, 2, 4]
The Idea
Think of the teeth of a saw, or a row of hills drawn by a child: up, down, up, down. That is the shape the array must take. Read Example 1's answer, [3, 5, 1, 6, 2, 4]
The input [3, 5, 2, 1, 6, 4]
No. Each position only has to agree with the number just before it. At an odd position (1, 3, 5) the number must be at least the one before it, a rise. At an even position (2, 4) it must be at most the one before it, a fall. So we can walk from left to right with a position ii
In the first row, the pairs up to position 2 are already right: 3 ≤ 5 and 5 ≥ 2. At i = 3[3, 5, 1, 2, 6, 4]i = 4
One thing in the second row deserves a closer look. The first swap moved 1 to position 2, so it changed a pair that was already checked, 5 and the number after it. That pair is still right, because 1 is even smaller than the 2 that was there, and 5 ≥ 1 holds.
The pair staying right is not luck. A swap at a rise only happens when the number at i - 1i - 1
Solution Approach
Turning that idea into code means settling three things: whether to sort at all, how each position knows which way it should go, and what to do with equal numbers.
Sorting first
Why not just sort? Sorting does give a correct answer: sort the array, then swap the numbers at positions 1 and 2, then at 3 and 4, and so on. [1, 2, 3, 4][1, 3, 2, 4]
The direction at each position
How does a position know whether it needs a rise or a fall? From whether it is odd or even: i % 2 == 1nums[i] < nums[i - 1]nums[i] > nums[i - 1]
Equal numbers
What if two neighbours are equal? The pattern uses ≤ and ≥, so equal neighbours already follow it, and nothing needs to move. That is why both checks above use a strict <>
A stricter version of the problem, where every rise and fall must be strict and neighbours may never be equal, is a different matter. This pass leaves equal neighbours in place, so that version needs another method.
Java Solution
The method wiggleSort
- Walk the array with one loop, moving
from position 1 to the end, since position 0 has no number before it.i - For each
, seti to true whenshouldRise is odd, and to false when it is even.i - If the pair
,nums[i - 1] goes the wrong way fornums[i] , swap the two numbers, using a temporary variableshouldRise to hold one of them.t - When the loop ends, the method returns nothing: it changed
itself.nums
The main
import java.util.Arrays;
public class WiggleSort {
public static void wiggleSort(int[] nums) {
for (int i = 1; i < nums.length; i++) {
boolean shouldRise = i % 2 == 1; // odd positions must be >= the value before them
if (shouldRise ? nums[i] < nums[i - 1] : nums[i] > nums[i - 1]) {
int t = nums[i]; // wrong direction: swap the two neighbours
nums[i] = nums[i - 1];
nums[i - 1] = t;
}
}
}
public static void main(String[] args) {
int[][] inputs = {
{3, 5, 2, 1, 6, 4},
{1, 2, 3, 4},
{6, 5, 4, 3, 2, 1},
{2, 2, 2},
{1, 1, 1, 2, 2, 2},
{7},
{}
};
for (int[] nums : inputs) {
String before = Arrays.toString(nums);
wiggleSort(nums);
System.out.println(before + " -> " + Arrays.toString(nums));
}
}
}
Running it prints:
[3, 5, 2, 1, 6, 4] -> [3, 5, 1, 6, 2, 4]
[1, 2, 3, 4] -> [1, 3, 2, 4]
[6, 5, 4, 3, 2, 1] -> [5, 6, 3, 4, 1, 2]
[2, 2, 2] -> [2, 2, 2]
[1, 1, 1, 2, 2, 2] -> [1, 1, 1, 2, 2, 2]
[7] -> [7]
[] -> []
[3, 5, 1, 6, 2, 4]i = 3i = 4
The third line starts from [6, 5, 4, 3, 2, 1][5, 6, 3, 4, 1, 2]
Look at the fifth line: [1, 1, 1, 2, 2, 2]
The original implementation,
Complexity
The work is one pass over the array, with a fixed amount of work at each position.
- Time: O(n), for an array of n numbers, because the loop checks each of the n - 1 neighbour pairs once and does at most one swap for each.
- Space: O(1). The numbers move inside
, and the only extra values arenums , the flagi and the swap variableshouldRise .t
A related problem that also arranges numbers by comparing each one with its neighbours is