In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "what are the common misunderstandings in the use of java binary search algorithm". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the common misunderstandings in the use of java binary search algorithm".
Directly use the binary search method to search for an unordered array or collection.
If you are lucky, you may find the data you want, but in most cases you can't get the results you want. Because binary search is to calculate the range of data you want to search according to the middle value, gradually narrowing the range each time. If your results are not in an ascending array, the data you are searching for is likely to be discarded and return a negative value. We can assert whether the array is ordered by a program like the one shown below.
For (int item0; I
< arr.length; i++) { if(arr[i]>Arr [iTun1]) {
Return 0
}
Return 1
}
I wrote a binary search algorithm and fell into an endless cycle. I don't know why.
This situation is caused because your boundary is not completely controlled. It is as follows:
Negative example 1:
Array `Pert [] = {12 int arr [] = 143pr 145pr 643 pr 23453 pr 233452}; `
Search data 233452 (directly in an endless loop)
Int start = 0
Int end = arr.length-1
While (start arr [mid]) {
Start = mid
} else {
Return mid
}
}
Return-1
If you analyze it carefully, you will find that the length of the array is 7.
* Intermediate mid=3 for the first cycle
* mid=4 in the middle of the second cycle
* Intermediate mid=5 of the third cycle
* the fourth cycle intermediate quantity mid=6
* Intermediate quantity mid=6 of the fifth cycle
* Intermediate quantity mid=6 of the sixth cycle
* Intermediate quantity mid=6 of the seventh cycle
*.
* thus fall into an endless cycle
Negative example 2:
If we pass the debugging, adjust the program to the following, loop the search results, and find that it is still an endless loop. Finally stuck in the median value of 5, the reason is that our median value can not be reduced all the time, so it eventually leads to this problem.
Int start = 0
Int end = arr.length-1
While (start arr [mid]) {
Start = mid-1
} else {
Return mid
}
}
Return-1
The correct way to write it is as follows: (the loop decreases during the iteration.)
Int start = 0
Int end = arr.length-1
While (start arr [mid]) {
Start = mid + 1
} else {
Return mid
}
}
Return-1
Thank you for your reading, the above is the "what is the common misunderstanding of the use of java binary search algorithm" content, after the study of this article, I believe you have a deeper understanding of what is the common misunderstanding of the use of java binary search algorithm, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.