Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function quickSort(arr) {
- if (arr.length <= 1) {
- return arr;
- }
- const less = [];
- const greater = [];
- const pivot = arr[arr.length - 1];
- for (let i = 0; i < arr.length - 1; ++i) {
- const num = arr[i];
- if (num < pivot) {
- less.push(num);
- } else {
- greater.push(num);
- }
- }
- return [...quickSort(less), pivot, ...quickSort(greater)];
- }
- const arr = [9, 4, 1, 6, 7, 3, 8, 2, 5];
- quickSort(arr);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement