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

Configure HTTPS for React applications running locally

If you build an application with create-react-app...

7 key features of 5G mobile phones

1. Support high-power terminals Compared with bas...

From trials to use cases, the big 5G stories of 2017

In 2017, 5G gradually moved from the laboratory t...

Cisco CEO: 5G will bring unexpected benefits to Cisco

[[278077]] Cisco is primarily known for its switc...

Ruijie Networks Completes SA-Based 5G Small Cell Test in China Mobile Laboratory

Ruijie Networks has always adhered to the concept...

Three considerations to spark innovation on the modern web

Today’s networks may not adapt well to changing n...

What does cutover mean in network engineering?

1. Main contents of this article What types of bu...

WiFi 7 is coming, but is it really reliable?

There is no fastest, only faster. WIFI6 has just ...

New opportunities brought by 5G millimeter wave fixed wireless

The broadband industry’s new mission is to extend...