Created
November 12, 2022 17:40
-
-
Save JuanmeGarcia/10fd545871e91d5753f9b06a51bb4022 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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