Use integer subproblem sizes and isolate the binary insertion demos (#2002)

Co-authored-by: Lukas <[email protected]>
This commit is contained in:
Lukas
2026-11-24 01:27:17 +08:00
committed by GitHub
parent 61ad6ed7f6
commit d5342f4ece
24 changed files with 240 additions and 192 deletions
@@ -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) {
}
/* 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 = {
@@ -100,7 +100,8 @@ function logRecur(n: number): number {
/* 线性对数阶 */
function linearLogRecur(n: number): number {
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++;
}
@@ -4,6 +4,8 @@
* Author: Gaofer Chou ([email protected])
*/
import { pathToFileURL } from 'node:url';
/* 二分查找插入点(无重复元素) */
function binarySearchInsertionSimple(
nums: Array<number>,
@@ -44,22 +46,24 @@ function binarySearchInsertion(nums: Array<number>, target: number): number {
}
/* 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 (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
// 无重复元素的数组
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);
}
}
export { binarySearchInsertion };
@@ -100,7 +100,8 @@ function logRecur(n) {
/* Linearithmic complexity */
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) {
}
/* Driver code */
// Array with no duplicate elements
let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
console.log('\nArray nums = ' + nums);
// Find an insertion point using binary search
for (const target of [6, 9]) {
const index = binarySearchInsertionSimple(nums, target);
console.log('Element ' + target + ' has insertion index ' + index);
}
if (require.main === module) {
// Array with no duplicate elements
let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
console.log('\nArray nums = ' + nums);
// Find an insertion point using binary search
for (const target of [6, 9]) {
const index = binarySearchInsertionSimple(nums, target);
console.log('Element ' + target + ' has insertion index ' + index);
}
// Array containing duplicate elements
nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
console.log('\nArray nums = ' + nums);
// Find an insertion point using binary search
for (const target of [2, 6, 20]) {
const index = binarySearchInsertion(nums, target);
console.log('Element ' + target + ' has insertion index ' + index);
// Array containing duplicate elements
nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
console.log('\nArray nums = ' + nums);
// Find an insertion point using binary search
for (const target of [2, 6, 20]) {
const index = binarySearchInsertion(nums, target);
console.log('Element ' + target + ' has insertion index ' + index);
}
}
module.exports = {
@@ -100,7 +100,8 @@ function logRecur(n: number): number {
/* Linearithmic complexity */
function linearLogRecur(n: number): number {
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++;
}
@@ -4,6 +4,8 @@
* Author: Gaofer Chou ([email protected])
*/
import { pathToFileURL } from 'node:url';
/* Find an insertion point using binary search (no duplicates) */
function binarySearchInsertionSimple(
nums: Array<number>,
@@ -44,22 +46,24 @@ function binarySearchInsertion(nums: Array<number>, target: number): number {
}
/* Driver code */
// Array with no duplicate elements
let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
console.log('\nArray nums = ' + nums);
// Find an insertion point using binary search
for (const target of [6, 9]) {
const index = binarySearchInsertionSimple(nums, target);
console.log('Element ' + target + ' has insertion index ' + index);
}
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
// Array with no duplicate elements
let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
console.log('\nArray nums = ' + nums);
// Find an insertion point using binary search
for (const target of [6, 9]) {
const index = binarySearchInsertionSimple(nums, target);
console.log('Element ' + target + ' has insertion index ' + index);
}
// Array containing duplicate elements
nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
console.log('\nArray nums = ' + nums);
// Find an insertion point using binary search
for (const target of [2, 6, 20]) {
const index = binarySearchInsertion(nums, target);
console.log('Element ' + target + ' has insertion index ' + index);
// Array containing duplicate elements
nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
console.log('\nArray nums = ' + nums);
// Find an insertion point using binary search
for (const target of [2, 6, 20]) {
const index = binarySearchInsertion(nums, target);
console.log('Element ' + target + ' has insertion index ' + index);
}
}
export { binarySearchInsertion };
@@ -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 = {
@@ -100,7 +100,8 @@ function logRecur(n: number): number {
/* 線形対数オーダー */
function linearLogRecur(n: number): number {
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++;
}
@@ -4,6 +4,8 @@
* Author: Gaofer Chou (gaofer-chou@qq.com)
*/
import { pathToFileURL } from 'node:url';
/* 二分探索による挿入位置の探索(重複要素がない場合) */
function binarySearchInsertionSimple(
nums: Array<number>,
@@ -44,22 +46,24 @@ function binarySearchInsertion(nums: Array<number>, target: number): number {
}
/* 実行用コード */
// 重複要素のない配列
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 (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
// 重複要素のない配列
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);
}
}
export { binarySearchInsertion };
@@ -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 = {
@@ -100,7 +100,8 @@ function logRecur(n: number): number {
/* 선형 로그 차수 */
function linearLogRecur(n: number): number {
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++;
}
@@ -4,6 +4,8 @@
* Author: Gaofer Chou (gaofer-chou@qq.com)
*/
import { pathToFileURL } from 'node:url';
/* 삽입 위치의 이진 탐색(중복 원소 없음) */
function binarySearchInsertionSimple(
nums: Array<number>,
@@ -44,22 +46,24 @@ function binarySearchInsertion(nums: Array<number>, target: number): number {
}
/* 실행 코드 */
// 중복 원소가 없는 배열
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 (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
// 중복 원소가 없는 배열
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);
}
}
export { binarySearchInsertion };
@@ -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) {
}
/* 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 = {
@@ -100,7 +100,8 @@ function logRecur(n: number): number {
/* Линейно-логарифмическая сложность */
function linearLogRecur(n: number): number {
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++;
}
@@ -4,6 +4,8 @@
* Author: Gaofer Chou (gaofer-chou@qq.com)
*/
import { pathToFileURL } from 'node:url';
/* Двоичный поиск точки вставки (без повторяющихся элементов) */
function binarySearchInsertionSimple(
nums: Array<number>,
@@ -44,22 +46,24 @@ function binarySearchInsertion(nums: Array<number>, target: number): number {
}
/* 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 (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
// Массив без повторяющихся элементов
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);
}
}
export { binarySearchInsertion };
@@ -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) {
}
/* 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 = {
@@ -100,7 +100,8 @@ function logRecur(n: number): number {
/* 線性對數階 */
function linearLogRecur(n: number): number {
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++;
}
@@ -4,6 +4,8 @@
* Author: Gaofer Chou (gaofer-chou@qq.com)
*/
import { pathToFileURL } from 'node:url';
/* 二分搜尋插入點(無重複元素) */
function binarySearchInsertionSimple(
nums: Array<number>,
@@ -44,22 +46,24 @@ function binarySearchInsertion(nums: Array<number>, target: number): number {
}
/* 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 (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
// 無重複元素的陣列
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);
}
}
export { binarySearchInsertion };