-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBinarySearch.ts
144 lines (132 loc) · 4.34 KB
/
BinarySearch.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* Copyright © https://github.com/jarry All rights reserved.
* @author: [email protected]
* @version: 1.0
*/
// 标准非递归版本,传递左右区间指针
class BinarySearch {
constructor() { }
search1(arr: Array<number>, target: number) {
var left = 0
var right = arr.length - 1
var midIndex: number, mid: number
// left是左侧,right是最右侧。搜索区间长度小于1时停止
while (left <= right) {
midIndex = Math.floor((left + right) / 2)
mid = arr[midIndex]
// 中间项等于目标项则返回下标
if (mid === target) {
return midIndex
} else if (target > mid) {
// 大于中间项折半查找右侧
left = midIndex + 1
} else {
// 小于中间项折半查找左侧
right = midIndex - 1
}
}
return -1
}
// 递归实现,移动左右区间指针
search2(arr: Array<number>, target: number, left: number = 0, right: number = -1) {
right = right === -1 ? arr.length - 1 : right
var midIndex = Math.floor((left + right) / 2)
var mid = arr[midIndex]
// 中间值等于查找项说明找到了,则返回中间项下标
if (target === mid) {
// 如果有重复项,返回第一个位置
if (arr[left] === target) {
return left
}
return midIndex
}
// 如果左侧与右侧相同,表面查找完毕,返回-1
if (left >= right) {
return -1
}
if (target > mid) {
// 折半右侧部分开始递归查找
return this.search2(arr, target, midIndex + 1, right)
} else {
// 折半左侧部分开始递归查找
return this.search2(arr, target, left, midIndex - 1)
}
}
// 解决存在重复项问题,返回第一个出现的位置。传递左右区间,向左查找(向右查找类似)
search3(arr: Array<number>, target: number) {
var left = 0
var right = arr.length - 1
var midIndex: number, mid: number
// 搜索区间小于等于1停止搜索
while (left <= right) {
// 找到中间值
midIndex = Math.floor((left + right) / 2)
mid = arr[midIndex]
if (target > mid) {
// 大于中间项折半查找右侧
left = midIndex + 1
} else {
// 小于等于中间项则折半查找左侧,同时缩小右侧范围
right = midIndex - 1
}
}
// 此时left是最左侧目标项
if (left < arr.length && arr[left] === target) {
return left
}
return -1
}
// 二分搜索递归查找,记录下右侧位置
search4(arr: Array<number>, target: number, resultIndex: number = 0) {
if (arr.length <= 1 && target !== arr[0]) {
return -1
}
let midIndex = Math.floor((arr.length - 1) / 2)
let mid = arr[midIndex]
resultIndex = resultIndex === undefined ? 0 : resultIndex
if (target === mid) {
return resultIndex + midIndex
} else if (target > mid) {
// 如果在右侧,则需要将中间index加上
return this.search4(arr.splice(midIndex + 1), target, resultIndex + midIndex + 1)
} else {
return this.search4(arr.splice(0, midIndex), target, resultIndex)
}
}
}
(function () {
console.time("time");
const binarySearch = new BinarySearch();
console.time('time')
var arr = [2, 3, 8, 10, 10, 13]
var result = binarySearch.search1(arr, 10) // 4
console.log('binarySearch.search1(arr, 10)', '=> result:' + result)
console.timeEnd('time')
console.time('time')
arr = [2, 3, 8, 10, 10, 13]
result = binarySearch.search2(arr, 10) // 3
console.log('binarySearch.search2(arr, 10)', '=> result:' + result)
console.timeEnd('time')
console.time('time')
arr = [2, 3, 8, 10, 10, 13]
result = binarySearch.search3(arr, 10) // 3
console.log('binarySearch.search3(arr, 10)', '=> result:' + result)
console.timeEnd('time')
console.time('time')
arr = [2, 3, 8, 10, 10, 13]
result = binarySearch.search4(arr, 10) // 4
console.log('binarySearch.search4(arr, 10)', '=> result:' + result)
console.timeEnd('time')
})();
/**
jarry@jarrys-MacBook-Pro binarysearch % tsc BinarySearch.ts
jarry@jarrys-MacBook-Pro binarysearch % node BinarySearch.js
binarySearch.search1(arr, 10) => result:4
time: 2.151ms
binarySearch.search2(arr, 10) => result:3
time: 0.195ms
binarySearch.search3(arr, 10) => result:3
time: 0.081ms
binarySearch.search4(arr, 10) => result:4
time: 0.099ms
*/