Wiggle sort

December 5, 2022

array-and-hashmap

Problem URL: Wiggle sort

We will loop over the array, that 2 elements per iteration, then sort 2 elements once ascending and then descending order and swap them. We will do this until we reach the end of the array.

class Solution:
    def wiggleSort(self, nums: List[int]) -> None:
        for i in range(len(nums)):
            nums[i:i+2] = sorted(nums[i:i+2], reverse=i%2)

Time complexity: O(n)
Space complexity: O(1)