forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request wangzheng0822#132 from nameczz/master
add [js] 15-16 binary find
- Loading branch information
Showing
2 changed files
with
117 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* 二分查找 | ||
* | ||
* Author: nameczz | ||
*/ | ||
// 数组必须有序 不存在重复 | ||
const biaryFind = (sortedArr, target) => { | ||
if (sortedArr.length === 0) return -1 | ||
let low = 0 | ||
let high = sortedArr.length - 1 | ||
while (low <= high) { | ||
const mid = Math.floor((low + high) / 2) | ||
if (target === sortedArr[mid]) { | ||
return mid | ||
} else if (target < sortedArr[mid]) { | ||
high = mid - 1 | ||
} else { | ||
low = mid + 1 | ||
} | ||
} | ||
return -1 | ||
} | ||
const arr = [1, 4, 5, 6, 7, 8, 10, 11, 23, 42, 44, 54, 56, 77, 102] | ||
console.log(biaryFind(arr, 44)) | ||
console.log(biaryFind(arr, 1)) | ||
console.log(biaryFind(arr, 102)) | ||
console.log(biaryFind(arr, 1111)) |
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,90 @@ | ||
/** | ||
* 二分查找 | ||
* | ||
* Author: nameczz | ||
*/ | ||
|
||
|
||
// 查找第一个等于给定值 | ||
const biaryFindFirst = (sortedArr, target) => { | ||
if (sortedArr.length === 0) return -1 | ||
let low = 0 | ||
let high = sortedArr.length - 1 | ||
while (low <= high) { | ||
const mid = Math.floor((low + high) / 2) | ||
|
||
if (target < sortedArr[mid]) { | ||
high = mid - 1 | ||
} else if (target > sortedArr[mid]) { | ||
low = mid + 1 | ||
} else { | ||
if (mid === 0 || sortedArr[mid - 1] < target) return mid | ||
high = mid - 1 | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
// 查找最后一个相等的数 | ||
const biaryFindLast = (sortedArr, target) => { | ||
if (sortedArr.length === 0) return -1 | ||
let low = 0 | ||
let high = sortedArr.length - 1 | ||
while (low <= high) { | ||
const mid = Math.floor((low + high) / 2) | ||
if (target < sortedArr[mid]) { | ||
high = mid - 1 | ||
} else if (target > sortedArr[mid]) { | ||
low = mid + 1 | ||
} else { | ||
if (mid === sortedArr.length - 1 || sortedArr[mid + 1] > target) return mid | ||
low = mid + 1 | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
// 查找第一个大于等于给定值的元素 | ||
const biaryFindFistBig = (sortedArr, target) => { | ||
if (sortedArr.length === 0) return -1 | ||
let low = 0 | ||
let high = sortedArr.length - 1 | ||
while (low <= high) { | ||
const mid = Math.floor((low + high) / 2) | ||
if (target <= sortedArr[mid]) { | ||
if (mid === 0 || sortedArr[mid - 1] < target) return mid | ||
high = mid - 1 | ||
} else { | ||
low = mid + 1 | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
// 查找最后一个小于等于给定值的元素 | ||
const biaryFindLastSmall = (sortedArr, target) => { | ||
if (sortedArr.length === 0) return -1 | ||
let low = 0 | ||
let high = sortedArr.length - 1 | ||
while (low <= high) { | ||
const mid = Math.floor((low + high) / 2) | ||
if (sortedArr[mid] > target) { | ||
high = mid - 1 | ||
} else { | ||
if (mid === sortedArr.length - 1 || sortedArr[mid + 1] > target) return mid | ||
low = mid + 1 | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
const arr = [1, 2, 3, 4, 4, 4, 4, 4, 6, 7, 8, 8, 9] | ||
const first = biaryFindFirst(arr, 4) | ||
console.log(`FindFirst: ${first}`) | ||
|
||
const last = biaryFindLast(arr, 4) | ||
console.log(`FindLast: ${last}`) | ||
const FisrtBig = biaryFindFistBig(arr, 5) | ||
console.log(`FindFisrtBig: ${FisrtBig}`) | ||
const LastSmall = biaryFindLastSmall(arr, 4) | ||
console.log(`FindLastSmall: ${LastSmall}`) |