forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.js
More file actions
Latest commit
30 lines (26 loc) · 705 Bytes
/
Copy pathQuickSort.js
File metadata and controls
30 lines (26 loc) · 705 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
/**
* @function QuickSort
* @description Quick sort is a comparison sorting algorithm that uses a divide and conquer strategy.
* @param {Integer[]} items - Array of integers
* @return {Integer[]} - Sorted array.
* @see [QuickSort](https://en.wikipedia.org/wiki/Quicksort)
*/
functionquickSort(items){
constlength=items.length
if(length<=1){
returnitems
}
constPIVOT=items[0]
constGREATER=[]
constLESSER=[]
for(leti=1;i<length;i++){
if(items[i]>PIVOT){
GREATER.push(items[i])
}else{
LESSER.push(items[i])
}
}
constsorted=[...quickSort(LESSER),PIVOT, ...quickSort(GREATER)]
returnsorted
}
export{quickSort}