-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathQuickSort.js
More file actions
31 lines (27 loc) · 974 Bytes
/
QuickSort.js
File metadata and controls
31 lines (27 loc) · 974 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
/* QuickSort works by picking a pivot and dividing the list in relation to that pivot:
all elements greater than the pivot go to the right of the pivot,
and all elements less than or equal to the pivot go to the left of the pivot.
Elements on both sides are quick sorted, and this is repeated until the complete list is sorted.
*/
function quickSort(list) {
if (list.length <= 1) {
return list
} else {
const left = []
const right = []
const sorted = []
const pivot = list.pop() // we're picking the last item to act as the pivot
const length = list.length
for (let i = 0; i < length; i++) {
if (list[i] <= pivot) {
left.push(list[i])
} else {
right.push(list[i])
}
}
return sorted.concat(quickSort(left), pivot, quickSort(right))
}
}
const list = [0,85,64,21,36]
const sorted = quickSort(list)
console.log(sorted)