MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / wiggle_sort

Function wiggle_sort

sorts/wiggle_sort.py:12–29  ·  view source on GitHub ↗

Python implementation of wiggle. Example: >>> wiggle_sort([0, 5, 3, 2, 2]) [0, 5, 2, 3, 2] >>> wiggle_sort([]) [] >>> wiggle_sort([-2, -5, -45]) [-45, -2, -5] >>> wiggle_sort([-2.1, -5.68, -45.11]) [-45.11, -2.1, -5.68]

(nums: list)

Source from the content-addressed store, hash-verified

10
11
12def wiggle_sort(nums: list) -> list:
13 """
14 Python implementation of wiggle.
15 Example:
16 >>> wiggle_sort([0, 5, 3, 2, 2])
17 [0, 5, 2, 3, 2]
18 >>> wiggle_sort([])
19 []
20 >>> wiggle_sort([-2, -5, -45])
21 [-45, -2, -5]
22 >>> wiggle_sort([-2.1, -5.68, -45.11])
23 [-45.11, -2.1, -5.68]
24 """
25 for i, _ in enumerate(nums):
26 if (i % 2 == 1) == (nums[i - 1] > nums[i]):
27 nums[i - 1], nums[i] = nums[i], nums[i - 1]
28
29 return nums
30
31
32if __name__ == "__main__":

Callers 1

wiggle_sort.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected