Briefly describe the binary search algorithm and time complexity, and implement a binary search algorithm

Briefly describe the binary search algorithm and time complexity, and implement a binary search algorithm

[[432404]]

Binary search is also called half-search algorithm. It is a simple and easy-to-understand fast search algorithm. For example, I randomly write a number between 0-100 and ask you to guess what I wrote. Every time you guess, I will tell you whether your guess is too high or too low, until you guess it right.

This algorithm requires that the array to be searched has been sorted, and the implementation steps are as follows:

  • Select the middle number in an array
  • The search number is compared with the middle number. If it is lower than the middle number, search in the subarray to the left of the middle number; if it is higher than the middle number, search in the subarray to the right of the middle number; if they are equal, return a successful search.
  • Repeat the previous step until the search succeeds or fails.
  1. function binarySearch(items, item) {
  2. var low = 0,
  3. high = items.length - 1,
  4. mid, elem
  5. while(low <= high) {
  6. mid = Math.floor((low+high)/2)
  7. elem = items[mid]
  8. if(elem < item) {
  9. low = mid + 1
  10. } else if(elem > item) {
  11. high = mid - 1
  12. } else {
  13. return mid
  14. }
  15. }
  16. return -1
  17. }
  18.  
  19. // test
  20. var arr = [2,3,1,4]
  21. // Quick sort
  22. quickSort(arr)
  23.  
  24. binarySearch(arr, 3)
  25. // 2
  26.  
  27. binarySearch(arr, 5)
  28. // -1

Test success

Binary search error-prone points:

  • The loop exit condition is low <= high. Note that it is <=
  • The value of mid is Math.floor((low+high)/2)
  • low high Each time it is updated, low = mid + 1 high = mid - 1

Binary search limitations:

  • The target object is an array structure, because the elements are randomly accessed through subscripts
  • The array must be in order
  • The array is too small to be suitable, just use sequential search
  • It is not appropriate to have an array that is too long. An array requires continuous memory space, and an array that is too long is not conducive to storage.

Time complexity: O(logn)

Space complexity: O(1)

leetcode: https://leetcode-cn.com/problems/binary-search/solution/er-fen-cha-zhao-by-user7746o/

<<:  Xiao Yaqing, Minister of Industry and Information Technology: Promote the deep integration of 5G, Internet, big data, artificial intelligence and manufacturing

>>:  Flink's general method for calculating Pv and Uv

Recommend

Will enterprises have dedicated 5G networks in the future?

5G networks bring many benefits to smartphone use...

Russian scientists propose data encoding method for 6G standard

Russian scientists propose data encoding method f...

Deutsche Telekom warns: Banning Huawei will hinder Europe's 5G development

Europe will fall behind the United States and Chi...

Working together: Two ways Wi-Fi and 5G can coexist

WBA: Wi-Fi and 5G coexist at the physical layer o...

RackNerd: $9.49/year KVM-768MB/12GB/2TB/San Jose and other data centers

RackNerd released a March promotion plan, includi...

What does the all-out war on IPv6 mean for China's Internet?

[[238041]] Image source: Visual China IPv4. What ...