Skip to content

Binary search is an efficient algorithm for finding a specific element in a sorted array.

Notifications You must be signed in to change notification settings

edgreyjo/binary-

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

binary-

Binary search is an efficient algorithm for finding a specific element in a sorted array. /**

  • Performs binary search on a sorted array to find the index of a target element.
  • @param {Array} array - The sorted array to search in.
  • @param {*} target - The element to search for.
  • @returns {number} - The index of the target element, or -1 if it is not found. */ function binarySearch(array, target) { let left = 0; let right = array.length - 1;

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

return -1; }

// Example usage: const sortedArray = [1, 3, 5, 7, 9, 11, 13]; const targetElement = 7; const targetIndex = binarySearch(sortedArray, targetElement);

console.log(The index of ${targetElement} is ${targetIndex}.);

About

Binary search is an efficient algorithm for finding a specific element in a sorted array.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published