Skip to content

Commit

Permalink
docs: update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
daiwanxing committed Jun 12, 2023
1 parent 9c087df commit 2adea17
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions docs/articles/algorithm/binary-search.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,31 @@ console.log(binarySearch([1, 3, 4, 6, 7, 9, 10, 13, 14], 1)); // 0

```js
function binarySearch(data, target) {
let value = -1;
let value = -1;

let start = 0;
let end = data.length;
let start = 0;
let end = data.length;

while (start < end) {
const middleIndex = Math.floor(start + (end - start) / 2);
const middleItem = data[middleIndex];
while (start <= end) {
const middleIndex = Math.floor(start + (end - start) / 2);
const middleItem = data[middleIndex];

if (target == middleItem) {
value = middleIndex;
break;
}
if (target == middleItem) {
value = middleIndex;
break;
}

if (target < middleItem) {
end = middleIndex - 1;
} else {
start = middleIndex + 1;
}
if (target < middleItem) {
end = middleIndex - 1;
} else {
start = middleIndex + 1;
}
}

if (end === start) value = middleIndex;
}
return value;
}

return
console.log(binarySearch([1, 3, 4, 6, 7, 9, 10, 13, 14], 9));
```

## 算法复杂度
Expand Down

0 comments on commit 2adea17

Please sign in to comment.