In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the principle and usage of binary search". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the principle and usage of binary search.
Concept
Binary search, also known as half search (Binary Search), is a more efficient search method. However, half-and-half lookup requires that the linear table must adopt a sequential storage structure, and the elements in the table must be arranged in order by keyword.
Premise of principle analysis
Array list structure that must be stored in the order of "big to small" or "small to large"
Search method
The list is halved, the middle element is compared with the target value, and after judgment, it is decided to leave the first half or the second half, and finally find the equivalent value.
A schematic diagram of the search process that defines the length of the array 12 and stores integers 1-12
Find the index with a value of 3
Find the subscript with a value of 13
Find the subscript with a value of 11
Public int binarySearch (int [] arrays, int searchTag, int left, int right) {int mid = (right + left) / 2; if (mid)
< 0 || mid >= arras.length) {return-1;} if (arrays [mid] = = searchTag) {return mid;} if (arrays [mid] > searchTag) {right = mid;} else if (arrays [mid]
< searchTag){ left = mid + 1; } if (left >= right) {return-1;} return binarySearch (arrays, searchTag, left, right);} cyclic public int binarySearch (int [] arrays, int searchTag) {int right = arrays.length, left = 0; while (left)
< right){ int mod = (right + left) / 2; if (arrays[mid] == searchTag){ return mid; } else if (arrays[mid] >SearchTag) {right = mid;} else {left = mid + 1;}} return-1;} at this point, I believe you have a deeper understanding of the principle and usage of binary search, so you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.