-
Notifications
You must be signed in to change notification settings - Fork 396
/
Copy pathwiggleSort.java
57 lines (41 loc) · 1.14 KB
/
wiggleSort.java
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
46
47
48
49
50
51
52
53
54
55
56
57
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3]....
Example:
Input: nums = [3,5,2,1,6,4]
Output: One possible answer is [3,5,1,6,2,4]
The rule is:
If i is even, nums[i] <= nums[i+1]
If i is odd, nums[i] >= nums[i+1]
Traverse the array, and fix the ordering by swapping unmatched pairs.
//TC: O(N)
//SC: O(1)
class Solution {
public void wiggleSort(int[] nums) {
boolean less = true;
for (int i = 0; i < nums.length - 1; i++) {
if (less) {
if (nums[i] > nums[i + 1]) {
swap(nums, i, i + 1);
}
} else {
if (nums[i] < nums[i + 1]) {
swap(nums, i, i + 1);
}
}
less = !less;
}
}
private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
SHORTER WAY, SAME LOGIC
public void wiggleSort(int[] nums) {
for (int i = 0; i < nums.length - 1; i++) {
if (((i % 2 == 0) && nums[i] > nums[i + 1])
|| ((i % 2 == 1) && nums[i] < nums[i + 1])) {
swap(nums, i, i + 1);
}
}
}