-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from PaxtonDevs/bSearch
BinarySearchSorted added for issue [#18]
- Loading branch information
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
programs/Javascript/reverse_string/binarySearchOnSortedArray.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
const binarySearchSorted = (arr, target) => { | ||
let start = 0; | ||
let end = arr.length - 1; | ||
|
||
while(start <= end) { | ||
|
||
let middle = Math.floor((start + end) / 2); | ||
|
||
if(target === arr[middle]) { | ||
return console.log('Target has been found!'); | ||
} | ||
|
||
if(target > arr[middle]) { | ||
start = middle + 1; | ||
} | ||
|
||
if(target < arr[middle]) { | ||
end = middle - 1; | ||
} | ||
|
||
} | ||
console.log('Search completed, target not found...') | ||
} | ||
|
||
|
||
binarySearchSorted([1,5,8,9,10,14,16,19], 8) |