Skip to content

Imshivamnair/JavaScript-Q21

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

JavaScript-Q21

Write a JavaScript program for binary search.

Sample array : [0,1,2,3,4,5,6] console.log(l.br_search(5)) will return '5'

// Extending the prototype of Array to add a binary search method Array.prototype.br_search = function (target) { // Calculate the index of the middle element var half = parseInt(this.length / 2);

// Base case: If the target is equal to the middle element, return the index if (target === this[half]) { return half; }

// Recursive case: If the target is greater than the middle element, search the right half if (target > this[half]) { return half + this.slice(half, this.length).br_search(target); } // Recursive case: If the target is less than the middle element, search the left half else { return this.slice(0, half).br_search(target); } };

// Example usage: Create an array and perform a binary search for the target value var l = [0, 1, 2, 3, 4, 5, 6]; console.log(l.br_search(5)); // Output: 5 (index of the target value)

Output: 5

About

Write a JavaScript program for binary search.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published