-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmeandering-array.js
More file actions
38 lines (32 loc) · 1.06 KB
/
meandering-array.js
File metadata and controls
38 lines (32 loc) · 1.06 KB
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
/*
Please write a function that will take in an array of random integers (including neagtive ones) and return an array with the biggest number first, smallest number second, second-biggest number third, second-smallest number fourth, etc.
EXAMPLES:
meanderingArray([3, 7, -2, 3, -8, 9]) // Should return [9, -8, 7, -2, 3, 3]
meanderingArray([4, -2, -3, 3]) // Should return [4, -3, 3, -2]
*/
function meanderingArray(unsorted) {
const sorted = quickSort(unsorted);
const finished = [];
while (sorted.length) {
const most = sorted.shift();
let least;
finished.push(most);
if (sorted.length) {
least = sorted.pop();
finished.push(least);
}
}
return finished;
}
function quickSort(data) {
if (data.length <= 1) {
return data;
} else {
const pivot = data[0];
const lesser = data.filter(num => num < pivot);
const equal = data.filter(num => num === pivot);
const greater = data.filter(num => num > pivot);
return quickSort(greater).concat(equal.concat(quickSort(lesser)));
}
}
module.exports = meanderingArray;