-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9710e05
commit cbb322b
Showing
5 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# 二分搜尋(Binary Search) | ||
|
||
## 雙閉區間 | ||
|
||
```ts | ||
function binarySearch(arr: number[], target: number): number { | ||
let low = 0; | ||
let high = arr.length - 1; | ||
|
||
while (low <= high) { | ||
const mid = Math.floor((low + high) / 2); | ||
|
||
// 找到目標,返回索引 | ||
if (arr[mid] === target) return mid; | ||
|
||
// 如果 arr[mid] 小於 target,則移動 low 指標 | ||
if (arr[mid] < target) { | ||
low = mid + 1; | ||
} | ||
|
||
// 如果 arr[mid] 大於 target,則移動 high 指標 | ||
if (arr[mid] > target) { | ||
high = mid - 1; | ||
} | ||
} | ||
|
||
return -1; // 找不到目標 | ||
} | ||
|
||
const sortedArray = [1, 2, 4, 7, 11, 16, 22, 29, 37, 46]; | ||
const target = 37; | ||
console.log(binarySearch(sortedArray, target)); // 8 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
function binarySearch(arr: number[], target: number): number { | ||
let low = 0; | ||
let high = arr.length - 1; | ||
|
||
while (low <= high) { | ||
const mid = Math.floor((low + high) / 2); | ||
|
||
// 找到目標,返回索引 | ||
if (arr[mid] === target) return mid; | ||
|
||
// 如果 arr[mid] 小於 target,則移動 low 指標 | ||
if (arr[mid] < target) { | ||
low = mid + 1; | ||
} | ||
|
||
// 如果 arr[mid] 大於 target,則移動 high 指標 | ||
if (arr[mid] > target) { | ||
high = mid - 1; | ||
} | ||
} | ||
|
||
return -1; // 找不到目標 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters