forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.js
32 lines (24 loc) · 824 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
function quickSort(unsortedArray, start, end){
let numPositions = end - start;
if(numPositions > 1){
let pivotPosition = partition(unsortedArray, start, end);
quickSort(unsortedArray, start, pivotPosition);
quickSort(unsortedArray, pivotPosition + 1, end);
}
return unsortedArray;
}
function partition(array, start, end){
let pivot = array[end - 1];
let foundSmaller = 0;
for (var i = start; i < end - 1; i++) {
if(array[i] <= pivot){
[array[i], array[foundSmaller]] = [array[foundSmaller], array[i]];
foundSmaller++;
}
}
[array[end - 1], array[foundSmaller]] = [array[foundSmaller], array[end - 1]];
return foundSmaller;
}
var unsortedArray = [54,42,11,33,24,99,77,80];
let sortedArrayViaQuickSort = quickSort(unsortedArray, 0, unsortedArray.length);
console.log(sortedArrayViaQuickSort);