Title: 腾讯:简述二分查找算法与时间复杂度,并实现一个二分查找算法 · Issue #83 · sisterAn/JavaScript-Algorithms · GitHub
Open Graph Title: 腾讯:简述二分查找算法与时间复杂度,并实现一个二分查找算法 · Issue #83 · sisterAn/JavaScript-Algorithms
X Title: 腾讯:简述二分查找算法与时间复杂度,并实现一个二分查找算法 · Issue #83 · sisterAn/JavaScript-Algorithms
Description: 二分查找也称折半查找算法,它是一种简单易懂的快速查找算法。例如我随机写0-100之间的一个数字,让你猜我写的是什么?你每猜一次,我就会告诉你猜的大了还是小了,直到猜中为止。 该算法要求待查找的数组已排序,实现步骤如下: 选择数组中的中间数 查找数与中间数对比,比中间数低,则去中间数左边的子数组中寻找;比中间数高,则去中间数右边的子数组中寻找;相等则返回查找成功 重复上一步,知道查找成功或失败 function binarySearch(items, item) { va...
Open Graph Description: 二分查找也称折半查找算法,它是一种简单易懂的快速查找算法。例如我随机写0-100之间的一个数字,让你猜我写的是什么?你每猜一次,我就会告诉你猜的大了还是小了,直到猜中为止。 该算法要求待查找的数组已排序,实现步骤如下: 选择数组中的中间数 查找数与中间数对比,比中间数低,则去中间数左边的子数组中寻找;比中间数高,则去中间数右边的子数组中寻找;相等则返回查找成功 重复上一步,知道查找成功或失败...
X Description: 二分查找也称折半查找算法,它是一种简单易懂的快速查找算法。例如我随机写0-100之间的一个数字,让你猜我写的是什么?你每猜一次,我就会告诉你猜的大了还是小了,直到猜中为止。 该算法要求待查找的数组已排序,实现步骤如下: 选择数组中的中间数 查找数与中间数对比,比中间数低,则去中间数左边的子数组中寻找;比中间数高,则去中间数右边的子数组中寻找;相等则返回查找成功 重复上一步,知道查找成功或失败...
Opengraph URL: https://github.com/sisterAn/JavaScript-Algorithms/issues/83
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"腾讯:简述二分查找算法与时间复杂度,并实现一个二分查找算法","articleBody":"二分查找也称折半查找算法,它是一种简单易懂的快速查找算法。例如我随机写0-100之间的一个数字,让你猜我写的是什么?你每猜一次,我就会告诉你猜的大了还是小了,直到猜中为止。\r\n\r\n该算法要求待查找的数组已排序,实现步骤如下:\r\n\r\n- 选择数组中的中间数\r\n- 查找数与中间数对比,比中间数低,则去中间数左边的子数组中寻找;比中间数高,则去中间数右边的子数组中寻找;相等则返回查找成功\r\n- 重复上一步,知道查找成功或失败\r\n\r\n```js\r\nfunction binarySearch(items, item) {\r\n var low = 0,\r\n high = items.length - 1,\r\n mid, elem\r\n while(low \u003c= high) {\r\n mid = Math.floor((low+high)/2)\r\n elem = items[mid]\r\n if(elem \u003c item) {\r\n low = mid + 1\r\n } else if(elem \u003e item) {\r\n high = mid - 1\r\n } else {\r\n return mid\r\n }\r\n }\r\n return -1\r\n}\r\n\r\n// 测试\r\nvar arr = [2,3,1,4]\r\n// 快排\r\nquickSort(arr)\r\n\r\nbinarySearch(arr, 3)\r\n// 2\r\n\r\nbinarySearch(arr, 5)\r\n// -1\r\n```\r\n\r\n测试成功\r\n\r\n**二分查找易错点:**\r\n\r\n- 循环退出条件是`low \u003c= high` ,注意是 `\u003c=`\r\n- `mid` 的取值是 `Math.floor((low+high)/2)` \r\n- `low` `high` 每次更新的时候,`low = mid + 1` `high = mid - 1`\r\n\r\n**二分查找局限性:**\r\n\r\n- 针对的对象是数组结构,因为是通过下标来随机访问元素\r\n- 数组必须有序\r\n- 数组太小不合适,直接使用顺序查找即可\r\n- 数组太长不合适,数组要求连续的内存空间,数组太长不利于存储\r\n\r\n**时间复杂度: O(logn)** \r\n\r\n**空间复杂度:O(1)**\r\n\r\n[leetcode](https://leetcode-cn.com/problems/binary-search/solution/er-fen-cha-zhao-by-user7746o/)","author":{"url":"https://github.com/sisterAn","@type":"Person","name":"sisterAn"},"datePublished":"2020-07-15T13:33:28.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/83/JavaScript-Algorithms/issues/83"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d8070a58-5673-c772-22d4-18f4888563f2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AEE6:C2480:20D1A0F:2D38D11:696ABBC8 |
| html-safe-nonce | 69ba46bee5f7697eab8920edf81e7357534b9aaf95435df351f68facdea154e5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRUU2OkMyNDgwOjIwRDFBMEY6MkQzOEQxMTo2OTZBQkJDOCIsInZpc2l0b3JfaWQiOiIzNDk2MDUzMjAyNjA5NjgzNDAwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 963451c96ef36ead8194a29bcc795dff46f06bb5588f7ef8f597c8cf364198d5 |
| hovercard-subject-tag | issue:657354321 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/sisterAn/JavaScript-Algorithms/83/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7de463aafea8b8dbc038729b982a7c764810409f573f2174c81de75144b8b113/sisterAn/JavaScript-Algorithms/issues/83 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7de463aafea8b8dbc038729b982a7c764810409f573f2174c81de75144b8b113/sisterAn/JavaScript-Algorithms/issues/83 |
| og:image:alt | 二分查找也称折半查找算法,它是一种简单易懂的快速查找算法。例如我随机写0-100之间的一个数字,让你猜我写的是什么?你每猜一次,我就会告诉你猜的大了还是小了,直到猜中为止。 该算法要求待查找的数组已排序,实现步骤如下: 选择数组中的中间数 查找数与中间数对比,比中间数低,则去中间数左边的子数组中寻找;比中间数高,则去中间数右边的子数组中寻找;相等则返回查找成功 重复上一步,知道查找成功或失败... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | sisterAn |
| hostname | github.com |
| expected-hostname | github.com |
| None | 46ce962e0e18113ea447391b6ace8b02d4d2861e57b4fbab3658698f73d8855b |
| turbo-cache-control | no-preview |
| go-import | github.com/sisterAn/JavaScript-Algorithms git https://github.com/sisterAn/JavaScript-Algorithms.git |
| octolytics-dimension-user_id | 19721451 |
| octolytics-dimension-user_login | sisterAn |
| octolytics-dimension-repository_id | 252061924 |
| octolytics-dimension-repository_nwo | sisterAn/JavaScript-Algorithms |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 252061924 |
| octolytics-dimension-repository_network_root_nwo | sisterAn/JavaScript-Algorithms |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 30300f30bb3949de255e84a146706a3bdb5c19c9 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width