Skip to content

Instantly share code, notes, and snippets.

@JuanmeGarcia
Created November 12, 2022 17:40
Show Gist options
  • Save JuanmeGarcia/10fd545871e91d5753f9b06a51bb4022 to your computer and use it in GitHub Desktop.
Save JuanmeGarcia/10fd545871e91d5753f9b06a51bb4022 to your computer and use it in GitHub Desktop.
function quickSort(nums) {
if (nums.length <= 1) return nums;
const pivot = nums[nums.length - 1];
const left = [];
const right = [];
for (let i = 0; i < nums.length - 1; i++) {
if (nums[i] < pivot) {
left.push(nums[i]);
} else {
right.push(nums[i]);
}
}
return [...quickSort(left), pivot, ...quickSort(right)];
}
const binarySearch = (id, array) => {
let min = 0
let max = array.length - 1
let index
let element
while(min <= max){
index = Math.floor((min + max) / 2)
element = array[index]
if(element.id < id){
min = index + 1
}else if(element.id > id){
max = index - 1
}else{
return element
}
}
return void 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment