Example analysis of binary search algorithm implemented in Java

This paper describes the implementation of binary search algorithm in Java. Share with you for your reference. The details are as follows:

1. Premise: the premise of binary search is that the array to be searched must be sorted. Our implementation here defaults to ascending order

2. Principle: divide the array into three parts, Followed by the median (the so-called median is the value in the middle of the array). Compare the value to be found with the median of the array. If it is less than the median, find it in front of the median. If it is greater than the median, find it after the median and return it directly when it is equal to the median. Then, there is a recursive process in turn, which continues to decompose the first half or the second half into three parts. Maybe It's not very clear. If you don't understand, you can find it online. From the description, it can be seen that this algorithm is suitable for recursive implementation, and those that can be recursive can be implemented by loop. Therefore, our implementation is divided into recursion and loop, and the algorithm can be understood according to the code

Implementation code:

Efficiency comparison:

The efficiency of cyclic binary search algorithm is higher than that of recursive binary search algorithm

I hope this article will be helpful to your Java programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>