Skip to content

Instantly share code, notes, and snippets.

@dandresfg
Created December 5, 2020 05:14
Show Gist options
  • Save dandresfg/7baaabc6bfa646e9d6cab877fefe56e5 to your computer and use it in GitHub Desktop.
Save dandresfg/7baaabc6bfa646e9d6cab877fefe56e5 to your computer and use it in GitHub Desktop.
This is my implementation of quicksort.
function quickSort(arr){
const size = arr.length;
if(size === 0) return [];
else {
let pivot = arr[0];
let left = [];
let right = [];
for(let i = 1; i<size;i++){
let item = arr[i];
if(item < pivot){
left.push(item);
} else {
right.push(item);
}
}
return [...quickSort(left), pivot, ...quickSort(right)];
}
}
quickSort([1,2,5,9,7,15,23]);
//(7) [1, 2, 5, 7, 9, 15, 23]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment