This file contains hidden or 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
| /** | |
| * Classic Binary Search (Model: "The Vise") | |
| * * Time Complexity: O(log n) | |
| * Space Complexity: O(1) | |
| */ | |
| export function binarySearch(nums: number[], target: number): number { | |
| let left = 0; | |
| let right = nums.length - 1; | |
| while (left <= right) { |
This file contains hidden or 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 binarySearch( array, item ) { | |
| let low = 0; | |
| let high = array.length - 1; | |
| while ( low <= high ) { | |
| let mid = Math.floor( ( low + high ) / 2 ); | |
| let guess = array[ mid ]; | |
| if ( guess > item ) { | |
| high = mid - 1; |