-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick-sort.js
More file actions
29 lines (22 loc) · 873 Bytes
/
quick-sort.js
File metadata and controls
29 lines (22 loc) · 873 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
function quickSort(array) {
// n is the length of the input array
if (array.length <= 1) {
return array;
}
let pivot = array.shift();
let left = array.filter((el) => el < pivot); // O(n) linear
let right = array.filter((el) => el >= pivot);
let leftSorted = quickSort(left); // O(log n) best case - O(n) worst case
let rightSorted = quickSort(right);
return [...leftSorted, pivot, ...rightSorted]; // O(n) linear
}
// O(n log n) time complexity best case
// O(n^2) time complexity worst case - when all the elements are sorted already
// O(n log n) space complexity best case FOR OUR IMPLEMENTATION
// O(n^2) space complexity worse case FOR OUR IMPLEMENTATION
// O(log n) space complexity for IN-PLACE QUICKSORT (BEST IMPLEMENTATION)
// pivot = 3
// array = [4,2,5]
// left = [2]
// right = [4,5]
// O((3n^2*(logn)) - 2n - 5) === O(n^2 * logn)