fix algoritm (#45)

This commit is contained in:
Dmitrii Rytikov
2017-12-02 23:15:23 +03:00
committed by Aditya Bhargava
parent 3499ab656a
commit dd0100e471

View File

@@ -3,10 +3,9 @@ const quickSort = (array) => {
return array; return array;
} }
const pivot = array[0]; const pivot = array[0];
const less = array.filter(item => item < pivot); const keysAreLessPivot = array.slice(1).filter(key => key <= pivot);
const greater = array.filter(item => item > pivot); const keysAreMorePivot = array.slice(1).filter(key => key > pivot);
return [...quickSort(keysAreLessPivot), pivot, ...quickSort(keysAreMorePivot)];
return [...quickSort(less), pivot, ...quickSort(greater)];
}; };
console.log(quickSort([10, 5, 2, 3])); // [2, 3, 5, 10] console.log(quickSort([10, 5, 2, 3])); // [2, 3, 5, 10]