-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquick-sort.js
More file actions
21 lines (16 loc) · 450 Bytes
/
Copy pathquick-sort.js
File metadata and controls
21 lines (16 loc) · 450 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
var quickSort = function(nums) {
if (!nums) return []
if (nums.length < 2) return nums
let pivot = Math.floor(nums.length / 2)
const leftNums = []
const rightNums = []
const center = nums[pivot]
for (let i = 0; i < nums.length; i++) {
if (center < nums[i]) {
leftNums.push(nums[i])
} else if (i !== pivot) {
rightNums.push(nums[i])
}
}
return quickSort(leftNums).concat([center], quickSort(rightNums))
}