Skip to content

Commit

Permalink
fix 002_search binarySearch
Browse files Browse the repository at this point in the history
  • Loading branch information
yko-git committed Mar 13, 2024
1 parent f730a47 commit 1d070b8
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions 004_algorithm/002_search.js
Expand Up @@ -31,18 +31,20 @@ function linearSearch(array, target) {

function binarySearch(array, target) {
// 配列の位置を設定
let index = -1;
let low = 0; //左端
let high = array.length - 1; //右端

//左端が右端以下の間(1つの要素に絞られるまで)は処理を続ける
while (low <= high) {
mid = low + high; // 真ん中の位置
guess = array[mid]; // 真ん中の要素
let mid = Math.floor((low + high) / 2); // 真ん中の位置
let guess = array[mid]; // 真ん中の要素

//真ん中の要素がtargetと合致したら
if (guess == target) {
//位置を返す
return mid;
index = mid;
return index;

//真ん中の要素がtargetより大きかったら
} else if (guess > target) {
Expand All @@ -52,7 +54,7 @@ function binarySearch(array, target) {
low = mid + 1;
}
}
return -1;
return index;
}

module.exports = {
Expand Down

0 comments on commit 1d070b8

Please sign in to comment.