# Problem 280: Wiggle Sort

> <https://leetcode.com/problems/wiggle-sort/#/description>

![](/files/-Lpv9zlmCAvjDxmFM5q7)

## 思路

> <https://discuss.leetcode.com/topic/57053/java-solution-with-explanation-thoughts/2>

这个讲解不错！

* 我们有四种情况

![](/files/-Lpv9zlo7On3ySbwccrx)

* 也就是说，对于奇数位 （实际上是 index 为奇数， 比如 nums 里面的第二个元素），它要看看是否比上一个大，如果不是

要交换；如果是偶数位，要看它是否比上一个元素小，否则要交换。

```java
public class Solution {
    public void wiggleSort(int[] nums) {
        if (nums == null || nums.length == 0) return;

        for (int i = 1; i < nums.length; i++) {
            if (i % 2 == 0 && nums[i] > nums[i - 1]) {
                swap(nums, i - 1, i);
            }
            if (i % 2 != 0 && nums[i] < nums[i - 1]) {
                swap(nums, i - 1, i);
            }
        }

    }

    private void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://liuyang89116.gitbook.io/my-leetcode-book/post_chapter_3_array/problem-280-wiggle-sort.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
