Skip to content

Commit cb5728e

Browse files
committed
add 15-16 binary find
1 parent d4eba0b commit cb5728e

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

javascript/15_binary/binaryFind.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* 二分查找
3+
*
4+
* Author: nameczz
5+
*/
6+
// 数组必须有序 不存在重复
7+
const biaryFind = (sortedArr, target) => {
8+
if (sortedArr.length === 0) return -1
9+
let low = 0
10+
let high = sortedArr.length - 1
11+
while (low <= high) {
12+
const mid = Math.floor((low + high) / 2)
13+
if (target === sortedArr[mid]) {
14+
return mid
15+
} else if (target < sortedArr[mid]) {
16+
high = mid - 1
17+
} else {
18+
low = mid + 1
19+
}
20+
}
21+
return -1
22+
}
23+
const arr = [1, 4, 5, 6, 7, 8, 10, 11, 23, 42, 44, 54, 56, 77, 102]
24+
console.log(biaryFind(arr, 44))
25+
console.log(biaryFind(arr, 1))
26+
console.log(biaryFind(arr, 102))
27+
console.log(biaryFind(arr, 1111))
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* 二分查找
3+
*
4+
* Author: nameczz
5+
*/
6+
7+
8+
// 查找第一个等于给定值
9+
const biaryFindFirst = (sortedArr, target) => {
10+
if (sortedArr.length === 0) return -1
11+
let low = 0
12+
let high = sortedArr.length - 1
13+
while (low <= high) {
14+
const mid = Math.floor((low + high) / 2)
15+
16+
if (target < sortedArr[mid]) {
17+
high = mid - 1
18+
} else if (target > sortedArr[mid]) {
19+
low = mid + 1
20+
} else {
21+
if (mid === 0 || sortedArr[mid - 1] < target) return mid
22+
high = mid - 1
23+
}
24+
}
25+
return -1
26+
}
27+
28+
// 查找最后一个相等的数
29+
const biaryFindLast = (sortedArr, target) => {
30+
if (sortedArr.length === 0) return -1
31+
let low = 0
32+
let high = sortedArr.length - 1
33+
while (low <= high) {
34+
const mid = Math.floor((low + high) / 2)
35+
if (target < sortedArr[mid]) {
36+
high = mid - 1
37+
} else if (target > sortedArr[mid]) {
38+
low = mid + 1
39+
} else {
40+
if (mid === sortedArr.length - 1 || sortedArr[mid + 1] > target) return mid
41+
low = mid + 1
42+
}
43+
}
44+
return -1
45+
}
46+
47+
// 查找第一个大于等于给定值的元素
48+
const biaryFindFistBig = (sortedArr, target) => {
49+
if (sortedArr.length === 0) return -1
50+
let low = 0
51+
let high = sortedArr.length - 1
52+
while (low <= high) {
53+
const mid = Math.floor((low + high) / 2)
54+
if (target <= sortedArr[mid]) {
55+
if (mid === 0 || sortedArr[mid - 1] < target) return mid
56+
high = mid - 1
57+
} else {
58+
low = mid + 1
59+
}
60+
}
61+
return -1
62+
}
63+
64+
// 查找最后一个小于等于给定值的元素
65+
const biaryFindLastSmall = (sortedArr, target) => {
66+
if (sortedArr.length === 0) return -1
67+
let low = 0
68+
let high = sortedArr.length - 1
69+
while (low <= high) {
70+
const mid = Math.floor((low + high) / 2)
71+
if (sortedArr[mid] > target) {
72+
high = mid - 1
73+
} else {
74+
if (mid === sortedArr.length - 1 || sortedArr[mid + 1] > target) return mid
75+
low = mid + 1
76+
}
77+
}
78+
return -1
79+
}
80+
81+
const arr = [1, 2, 3, 4, 4, 4, 4, 4, 6, 7, 8, 8, 9]
82+
const first = biaryFindFirst(arr, 4)
83+
console.log(`FindFirst: ${first}`)
84+
85+
const last = biaryFindLast(arr, 4)
86+
console.log(`FindLast: ${last}`)
87+
const FisrtBig = biaryFindFistBig(arr, 5)
88+
console.log(`FindFisrtBig: ${FisrtBig}`)
89+
const LastSmall = biaryFindLastSmall(arr, 4)
90+
console.log(`FindLastSmall: ${LastSmall}`)

0 commit comments

Comments
 (0)