JavaScript Algorithms - 17 - Binary Search Solution

preview_player
Показать описание

📱 Follow Codevolution

Binary Search Solution
JavaScript Algorithms
Algorithms with JavaScript
Рекомендации по теме
Комментарии
Автор

best algorithms course so far please do more problem solving playlists

ilyesilyes
Автор

I think it's useful to include a line that converts whatever array you passed into this into a sorted array, to account for the user possibly entering in a nonsorted array. Something like "arr.sort((a, b) => a - b);"

stevenedwards
Автор

Ur middle index can and should be a const since each time the loop iterates the const mid is made again.

daymenpollet
Автор

Cant wait for RTK Query Course from you Vizwaz!.. thank you so much!

mamlzy
Автор

I guess my below solution is BigO- O(1) and even if the array is not sorted it is still returning the index,
Solution :-
function binaryArray(arr, target){
let oddNum = arr.length
if(oddNum % 2 !== 0){
for (let i = 0; i < arr.length; i++) {
if(arr.includes(target)){
let center = Math.ceil(arr.length/2) - 1
let arrayCenter = arr[center]

if(arrayCenter > target){
if(arr[i] === target) return i
}

if(arrayCenter === target){
return arrayCenter
}

if(arrayCenter < target){
if(arr[i] === target) return i
}
} else{
return -1
}

}
} else {
console.log("length is even")
return -1
}
}

console.log("1st case :", binaryArray([1, 2, 6, 10, 25, 69], 10))
console.log("2nd case :", binaryArray([1, 2, 5, 10, 25], 100))
console.log("3rd case :", binaryArray([1, 2, 4, 10, 25, 78, 1000], 1000))
console.log("4th case :", binaryArray([1, 2, 4, 10, 25, 78, 1000], 10))
console.log("5th case :", binaryArray([1, 2, 4, 10, 25, 78, 1000], 78))
console.log("6th case :", binaryArray([1, 2, 4, 10, 25, 78, 1000, 56, 2000, 69552, 352455, 2585545, 25505478], 2585545))

sir why should I not use this approach of above, then please tell me, how can I improve.

metaltank
Автор

Hi Sir,
Can I use below statement for sorting for this Binary search.

Let sort = array.sort((a, b) => a-b);

anandrajagopal
Автор

Thank you sir! This video very help me to understand a concept of algorithm :)

farhannawwafal_mipa_km
Автор

You explain things clearly! Thank you!

BrazilDan
Автор

That was clear and helpful, thanks so much!

israamoqbel
Автор

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]

function binarySearch(t) {
const half = Math.floor(arr.length / 2)

if (t === half) return half-1

if (t < half) {
for (let i = 0; i < arr[half +1]; i++) {
if (arr[i] === t) {
return i
}
}
}


if (t > half) {
for (let i = half; i < arr.length; i++) {
if (arr[i] === t) {
return i
}
}
}
}

Dan-ress
Автор

The word 'pointer' gave me a PTSD of coding in C 😂

Xraxus_
Автор

//This is a binary search but can also handle unsorted array as i'have sorted the array in the fuction. can anyone say whether it will increase time complexity or it will remain same as the solution in the video..

const binarySearch = (arr, target) => {
//sorting the array:

arrSorted = arr.sort((a, b) => a - b);
console.log(arrSorted);

let leftIndex = 0;
let rightIndex = arrSorted.length - 1;
if (arrSorted.length === 0) {
console.log("Array is empty.");
return -1;
}

while (leftIndex <= rightIndex) {
let middleIndex = Math.floor((leftIndex + rightIndex) / 2);
if (target === arrSorted[middleIndex]) {
console.log(`The index of ${target} is ${middleIndex}`);
return middleIndex;
} else if (target < arrSorted[middleIndex]) {
rightIndex = middleIndex - 1;
} else if (target > arrSorted[middleIndex]) {
leftIndex = middleIndex + 1;
}
}
console.log(`${target} is not in the array.`);
return -1;
};

console.log(binarySearch([], 7));
console.log(binarySearch([1, 2, 3, 7, 5, 6, 4], 1));
console.log(binarySearch([1, 22, 13, 4, 15, -46, 79], -46));
console.log(binarySearch([10, 62, 3, 41, 5, 66, 37], 11));

shanu
Автор

why some right and left if left is always zero?

felipeluiz
Автор

Can i ask? Its okay to use 'For Loop?

gibermarregalado
Автор

Thank You sir
Can we connect on Linkedin?

ankushladani
Автор

what if the count of the array is even, how to find the middle element?

RAVIRAJLADHA
Автор

function checkIfArrIsSortedInAsc(arr) {
for (let index in arr) {
if (arr[index] > arr[+index + 1]) return false;
}
return true;
}
function checkIfArrIsSortedInDesc(arr) {
for (let index in arr) if (arr[index] < arr[+index + 1]) return false;
return true;
}
function checkIfArrSorted(arr) {
return checkIfArrIsSortedInAsc(arr) ||
}
function binarySearch(arr, valueToFind) {
if (!checkIfArrSorted(arr)) return "Invalid Array you should sorted first : \n";
let left = 0;
let right = arr.length - 1;
let mid = (left + right) / 2;

while (left <= right) {
if (arr[mid] == valueToFind) return mid;
if {
if (arr[mid] < valueToFind) left = mid + 1;
else right = mid - 1;
} else {
if (arr[mid] > valueToFind) left = mid + 1;
else right = mid - 1;
}
mid = Math.floor((left + right) / 2);
}
return -1;
}

let arrAsc = [1, 1, 3, 4, 4, 5, 6];
let arrDesc = arrAsc.slice().reverse();
console.log(binarySearch(arrAsc, 1));
console.log(binarySearch(arrDesc, 1));

Ayoubmajid-uuyv
Автор

why can't we use for loop instead of while?

advaithgps
Автор

but here in this question you are not sorting

ahmedthousi
Автор

What if the array is [-10, 6, 6, 6, 10] ? Index should be 1 but this algo give 2....

saintthomastaquin
join shbcf.ru