Wiggle Sort

• I think we should address the second solution's core idea: greedy, rather than just pasting code and a small introduction what does the loop do

• Sure, I will revise the article to add more explanation. Please stay tuned.

• Could you explain the case, What if the requirement is like this nums[0] < nums[1] > nums[2] < nums[3] ....?

• @ashiqimran Yes that is a totally different problem, which is in Wiggle Sort II - https://leetcode.com/problems/wiggle-sort-ii/

• Yeah, Actually I am having trouble to solve it in place. Could you help me on Wiggle Sort II?

• @ashiqimran You can take a look at the highest voted answers on Wiggle Sort II here.

• @1337c0d3r Thanks :)

• Thanks for the analysis. I have a question, I tried to run all your solutions with this test case [1,2,2,1,2,1,1,1,1,3,2,2] but they produce the in correct result.
Ideally, it should show the result as [1, 3, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2] but it does not. Can you comment on this test case?

• @hieu.trinh You are saying Wiggle Sort II. See the comments of ashiqimran and of 1337c0d3r.

• I literally did it that way and it says Time Limit exceded :/

• @rbacevedo No you didn't. Don't lie.

• For the second approach, it said in the worst case we swap at most n/2 times. But for the example input, number of swaps seems to be n-1.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.