forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WiggleSort.swift
45 lines (39 loc) · 1.29 KB
/
WiggleSort.swift
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
34
35
36
37
38
39
40
41
42
43
44
45
/**
* Question Link: https://leetcode.com/problems/wiggle-sort/
* Primary idea: Iterate the array and swap the largest one to the middle
* Time Complexity: O(n), Space Complexity: O(1)
*/
class WiggleSort {
func wiggleSort(inout nums: [Int]) {
guard nums.count >= 2 else {
return
}
for i in 1.stride(to: nums.count, by: 2) {
let largestIndex = _getLargest(&nums, i - 1, i, i + 1)
if i != largestIndex {
_swap(&nums, largestIndex, i)
}
}
}
private func _swap<T>(inout nums: Array<T>, _ p: Int, _ q: Int) {
let temp = nums[p]
nums[p] = nums[q]
nums[q] = temp
}
private func _getLargest(inout nums: [Int], _ x: Int, _ y: Int, _ z: Int) -> Int {
let xVal = _isValid(x, nums.count) ? nums[x] : Int.min
let yVal = _isValid(y, nums.count) ? nums[y] : Int.min
let zVal = _isValid(z, nums.count) ? nums[z] : Int.min
let maxVal = max(xVal, yVal, zVal)
if maxVal == xVal {
return x
} else if maxVal == yVal {
return y
} else {
return z
}
}
private func _isValid(index: Int, _ len: Int) -> Bool {
return index >= 0 && index < len
}
}