-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.js
More file actions
31 lines (27 loc) · 777 Bytes
/
quickSort.js
File metadata and controls
31 lines (27 loc) · 777 Bytes
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
// Sort Integers II
// Given an integer array, sort it in ascending order.Use quick sort, merge sort, heap sort or any O(nlogn) algorithm.
// Example
// Given[3, 2, 1, 4, 5], return [1, 2, 3, 4, 5].
// QUICK SORT
var sortIntegers2 = function (A) {
quickSort(A, 0, A.length - 1)
}
var quickSort = (A, start, end) => {
if (start >= end) return
let left = start
let right = end
let pivot = A[start]
// partition
while (left <= right) {
while (left <= right && A[left] < pivot) left++
while (left <= right && A[right] > pivot) right--
if (left <= right) {
[A[left], A[right]] = [A[right], A[left]]
left++
right--
}
}
// [start, end] => [start, right] [left, end]
quickSort(A, start, right)
quickSort(A, left, end)
}