mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-24 23:50:45 +08:00
Use integer subproblem sizes and isolate the binary insertion demos (#2002)
Co-authored-by: Lukas <[email protected]>
This commit is contained in:
@@ -100,7 +100,8 @@ function logRecur(n) {
|
||||
/* 線形対数オーダー */
|
||||
function linearLogRecur(n) {
|
||||
if (n <= 1) return 1;
|
||||
let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
|
||||
const half = Math.floor(n / 2);
|
||||
let count = linearLogRecur(half) + linearLogRecur(half);
|
||||
for (let i = 0; i < n; i++) {
|
||||
count++;
|
||||
}
|
||||
|
||||
@@ -41,22 +41,24 @@ function binarySearchInsertion(nums, target) {
|
||||
}
|
||||
|
||||
/* 実行用コード */
|
||||
// 重複要素のない配列
|
||||
let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
|
||||
console.log('\n配列 nums = ' + nums);
|
||||
// 二分探索による挿入位置の探索
|
||||
for (const target of [6, 9]) {
|
||||
const index = binarySearchInsertionSimple(nums, target);
|
||||
console.log('要素 ' + target + ' の挿入位置のインデックスは ' + index);
|
||||
}
|
||||
if (require.main === module) {
|
||||
// 重複要素のない配列
|
||||
let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
|
||||
console.log('\n配列 nums = ' + nums);
|
||||
// 二分探索による挿入位置の探索
|
||||
for (const target of [6, 9]) {
|
||||
const index = binarySearchInsertionSimple(nums, target);
|
||||
console.log('要素 ' + target + ' の挿入位置のインデックスは ' + index);
|
||||
}
|
||||
|
||||
// 重複要素を含む配列
|
||||
nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
|
||||
console.log('\n配列 nums = ' + nums);
|
||||
// 二分探索による挿入位置の探索
|
||||
for (const target of [2, 6, 20]) {
|
||||
const index = binarySearchInsertion(nums, target);
|
||||
console.log('要素 ' + target + ' の挿入位置のインデックスは ' + index);
|
||||
// 重複要素を含む配列
|
||||
nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
|
||||
console.log('\n配列 nums = ' + nums);
|
||||
// 二分探索による挿入位置の探索
|
||||
for (const target of [2, 6, 20]) {
|
||||
const index = binarySearchInsertion(nums, target);
|
||||
console.log('要素 ' + target + ' の挿入位置のインデックスは ' + index);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
Reference in New Issue
Block a user