square brackets fix (#263)
This commit is contained in:
@@ -6,19 +6,19 @@
|
|||||||
* @returns {Array} Sorted array
|
* @returns {Array} Sorted array
|
||||||
*/
|
*/
|
||||||
function quicksort(array) {
|
function quicksort(array) {
|
||||||
// base case, arrays with 0 or 1 element are already "sorted"
|
// base case, arrays with 0 or 1 element are already "sorted"
|
||||||
if (array.length < 2) return array;
|
if (array.length < 2) return array;
|
||||||
// recursive case
|
// recursive case
|
||||||
let pivot = array[0];
|
let pivot = array[0];
|
||||||
// sub-array of all the elements less than the pivot
|
// sub-array of all the elements less than the pivot
|
||||||
let less = array.slice(1).filter(function(el) {
|
let less = array.slice(1).filter(function (el) {
|
||||||
return el <= pivot;
|
return el <= pivot;
|
||||||
});
|
});
|
||||||
// sub-array of all the elements greater than the pivot
|
// sub-array of all the elements greater than the pivot
|
||||||
let greater = array.slice(1).filter(function(el) {
|
let greater = array.slice(1).filter(function (el) {
|
||||||
return el > pivot;
|
return el > pivot;
|
||||||
});
|
});
|
||||||
return quicksort(less).concat([pivot], quicksort(greater));
|
return quicksort(less).concat(pivot, quicksort(greater));
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(quicksort([10, 5, 2, 3]));
|
console.log(quicksort([10, 5, 2, 3]));
|
||||||
|
|||||||
Reference in New Issue
Block a user