mirror of
https://github.com/krahets/hello-algo.git
synced 2026-12-02 20:01:36 +08:00
Use integer subproblem sizes and isolate the binary insertion demos (#2002)
Co-authored-by: Lukas <[email protected]>
This commit is contained in:
@@ -41,22 +41,24 @@ function binarySearchInsertion(nums, target) {
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
// 无重复元素的数组
|
||||
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