Skip to content

Commit

Permalink
Merge pull request #83 from PaxtonDevs/bSearch
Browse files Browse the repository at this point in the history
BinarySearchSorted added for issue  [#18]
  • Loading branch information
ademclk authored Oct 11, 2022
2 parents e9b7d8e + 0571c6f commit 0a26541
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions programs/Javascript/reverse_string/binarySearchOnSortedArray.js
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)

0 comments on commit 0a26541

Please sign in to comment.