forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwiggle_sort.py
More file actions
33 lines (24 loc) · 753 Bytes
/
wiggle_sort.py
File metadata and controls
33 lines (24 loc) · 753 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""
Wiggle Sort
Given an unsorted array, reorder it in-place such that
nums[0] < nums[1] > nums[2] < nums[3] ...
Reference: https://leetcode.com/problems/wiggle-sort/
Complexity:
Time: O(n) best / O(n) average / O(n) worst
Space: O(1)
"""
from __future__ import annotations
def wiggle_sort(array: list[int]) -> list[int]:
"""Reorder *array* in-place into wiggle-sorted order.
Args:
array: List of integers to reorder.
Returns:
The wiggle-sorted list.
Examples:
>>> wiggle_sort([3, 5, 2, 1, 6, 4])
[3, 5, 1, 6, 2, 4]
"""
for i in range(len(array)):
if (i % 2 == 1) == (array[i - 1] > array[i]):
array[i - 1], array[i] = array[i], array[i - 1]
return array