forked from alexkutzke/Javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.js
51 lines (43 loc) · 928 Bytes
/
QuickSort.js
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
/*
* Quick sort is a comparison sorting algorithm that uses a divide and conquer strategy.
* For more information see here: https://en.wikipedia.org/wiki/Quicksort
*/
/*
* Doctests
*
* > quickSort([5, 4, 3, 10, 2, 1])
* [1, 2, 3, 4, 5, 10]
* > quickSort([])
* []
* > quickSort([5, 4])
* [4, 5]
* > quickSort([1, 2, 3])
* [1, 2, 3]
*/
function quickSort (items) {
const length = items.length
if (length <= 1) {
return items
}
const PIVOT = items[0]
const GREATER = []
const LESSER = []
for (let i = 1; i < length; i++) {
if (items[i] > PIVOT) {
GREATER.push(items[i])
} else {
LESSER.push(items[i])
}
}
let sorted = quickSort(LESSER)
sorted.push(PIVOT)
sorted = sorted.concat(quickSort(GREATER))
return sorted
}
// Implementation of quick sort
let ar = [0, 5, 3, 2, 2]
// Array before Sort
console.log(ar)
ar = quickSort(ar)
// Array after sort
console.log(ar)