forked from TheAlgorithms/Rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiggle_sort.rs
54 lines (49 loc) · 1.28 KB
/
wiggle_sort.rs
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
//Wiggle Sort.
//Given an unsorted array nums, reorder it such
//that nums[0] < nums[1] > nums[2] < nums[3]....
//For example:
//if input numbers = [3, 5, 2, 1, 6, 4]
//one possible Wiggle Sorted answer is [3, 5, 1, 6, 2, 4].
pub fn wiggle_sort(nums: Vec<i32>) -> Vec<i32> {
//Rust 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]
let len = nums.len();
let mut p = nums;
for i in 1..len {
let num_x = p[i - 1];
let num_y = p[i];
if (i % 2 == 1) == (num_x > num_y) {
p[i - 1] = num_y;
p[i] = num_x;
}
}
return p
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn wingle_elements() {
let arr = vec![3, 5, 2, 1, 6, 4];
let res = wiggle_sort(arr.clone());
assert_eq!(res, [3, 5, 1, 6, 2, 4]);
}
#[test]
fn odd_number_of_elements() {
let arr = vec![4, 1, 3, 5, 2];
let res = wiggle_sort(arr.clone());
assert_eq!(res, [1, 4, 3, 5, 2]);
}
#[test]
fn repeated_elements() {
let arr = vec![5, 5, 5, 5];
let res = wiggle_sort(arr.clone());
assert_eq!(res, [5, 5, 5, 5]);
}
}