From 4f36e60af52b4bfd1a51a1074a5a954f3210c2bd Mon Sep 17 00:00:00 2001 From: yko-git Date: Mon, 11 Mar 2024 11:49:46 +0900 Subject: [PATCH 1/4] fix 004_algorithm/002_search linearSearch/binarySearch --- 004_algorithm/002_search.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/004_algorithm/002_search.js b/004_algorithm/002_search.js index 86ee651..e8786bc 100644 --- a/004_algorithm/002_search.js +++ b/004_algorithm/002_search.js @@ -1,4 +1,3 @@ - /** * 2.3.1 リニアサーチ * @@ -11,7 +10,15 @@ * [5, 3, 2, 1], 6 => -1 */ -function linearSearch (array, target) { +function linearSearch(array, target) { + let index = -1; + + for (let i = 0; i < array.length; i++) { + if (array[i] === target) { + index = i; + } + } + return index; } /** @@ -24,10 +31,12 @@ function linearSearch (array, target) { * [1, 2, 3, 4] 5 => -1 */ -function binarySearch (array, target) { +function binarySearch(array, target) { + let arrayLength = array.length / 2; + console.log(arrayLength); } module.exports = { linearSearch, - binarySearch -} + binarySearch, +}; From a52a8f39adec7a3c1d4ba80de8e84c8977549551 Mon Sep 17 00:00:00 2001 From: yko-git Date: Tue, 12 Mar 2024 12:22:13 +0900 Subject: [PATCH 2/4] fix 002_search linearSearch/binarySearch --- 004_algorithm/002_search.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/004_algorithm/002_search.js b/004_algorithm/002_search.js index e8786bc..68863d0 100644 --- a/004_algorithm/002_search.js +++ b/004_algorithm/002_search.js @@ -11,14 +11,12 @@ */ function linearSearch(array, target) { - let index = -1; - for (let i = 0; i < array.length; i++) { if (array[i] === target) { - index = i; + return i; } } - return index; + return -1; } /** @@ -32,8 +30,21 @@ function linearSearch(array, target) { */ function binarySearch(array, target) { - let arrayLength = array.length / 2; - console.log(arrayLength); + let mid = Math.floor(array.length / 2); + let index = -1; + + for (let i = 0; i < mid; i++) { + if (array[i] === target) { + return (index = i); + } + + for (let j = array.length; j >= mid; j--) { + if (array[j] === target) { + return (index = j); + } + } + return index; + } } module.exports = { From f730a4746356ffe0e1815ab777e3e717145db4b1 Mon Sep 17 00:00:00 2001 From: yko-git Date: Tue, 12 Mar 2024 16:14:18 +0900 Subject: [PATCH 3/4] fix 002_search binarySearch --- 004_algorithm/002_search.js | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/004_algorithm/002_search.js b/004_algorithm/002_search.js index 68863d0..a54c533 100644 --- a/004_algorithm/002_search.js +++ b/004_algorithm/002_search.js @@ -30,21 +30,29 @@ function linearSearch(array, target) { */ function binarySearch(array, target) { - let mid = Math.floor(array.length / 2); - let index = -1; + // 配列の位置を設定 + let low = 0; //左端 + let high = array.length - 1; //右端 - for (let i = 0; i < mid; i++) { - if (array[i] === target) { - return (index = i); - } + //左端が右端以下の間(1つの要素に絞られるまで)は処理を続ける + while (low <= high) { + mid = low + high; // 真ん中の位置 + guess = array[mid]; // 真ん中の要素 - for (let j = array.length; j >= mid; j--) { - if (array[j] === target) { - return (index = j); - } + //真ん中の要素がtargetと合致したら + if (guess == target) { + //位置を返す + return mid; + + //真ん中の要素がtargetより大きかったら + } else if (guess > target) { + //右端の位置は要素の位置-1 + high = mid - 1; + } else { + low = mid + 1; } - return index; } + return -1; } module.exports = { From 1d070b8932b6a3c7b644ca2a6f4d9f61d87115a4 Mon Sep 17 00:00:00 2001 From: yko-git Date: Wed, 13 Mar 2024 14:55:21 +0900 Subject: [PATCH 4/4] fix 002_search binarySearch --- 004_algorithm/002_search.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/004_algorithm/002_search.js b/004_algorithm/002_search.js index a54c533..5e73518 100644 --- a/004_algorithm/002_search.js +++ b/004_algorithm/002_search.js @@ -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) { @@ -52,7 +54,7 @@ function binarySearch(array, target) { low = mid + 1; } } - return -1; + return index; } module.exports = {