Java implementation of binary search algorithm

Source: Internet
Author: User

Algorithm

If there is a set of numbers for 3,12,24,36,55,68,75,88 to look up a given value of 24. Three variables can be set front,mid,end respectively to the upper bound of the data, the middle and lower bounds, mid= (front+end)/2.

1. Start front=0 (point 3), end=7 (Point 88), then mid=3 (point to 36). Because of mid>x, it should be found in the first half of the paragraph. 2. Make the new end=mid-1=2, while the front=0 is unchanged, then the new mid=1. At this point x>mid, it should be found in the second half of the paragraph. 3. Make the new front=mid+1=2, and end=2 unchanged, then the new mid=2, at this time a[mid]=x, find success. If the number to be looked up is not the number in the series, for example X=25, when the third judgment, X>a[mid], according to the above rules, so that front=mid+1, that is, front=3, the case of front>end, indicating that the search is unsuccessful.

Example: Find the data x that the user has entered into an array with n elements in order. the algorithm is as follows:

1. Determine the lookup range front=0,end=n-1, calculate the item mid= (front+end)/2.

2. If a[mid]=x or front>=end, end the lookup; otherwise, continue down.

3. If the a[mid]<x indicates that the element value to be found is only possible in the range larger than the middle item element, assign the value of Mid+1 to front and recalculate mid, go to step 2, and if a[mid]>x, the element value to be found may only be within the range smaller than the middle item element, Assign the value of Mid-1 to end and recalculate mid to go to step 2.

[one-dimensional array, binary find]2 algorithm complexity analysis time complexity

1. Worst case finding last element (or first element) Master theorem t (n) =t (N/2) +o (1) so T (n) =o (LOGN)
2. Best case Find Intermediate element O (1) The element that is found is the middle element (the middle of the odd-length sequence, the left-hand element of the even-length sequence)

Complexity of space:

S (n) =n

Java Implementation Code

Package Com.leo.kang.interview;public class BinarySearch {//Lookup count static int count;/** * @param args */public static void M Ain (string[] args) {//TODO auto-generated method stubint[] Array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; System.out.println (searchrecursive (array, 0, array.length-1, 9)); System.out.println (count); count = 0; System.out.println (Searchloop (array, 9)); System.out.println (count);}            /** * Performs a recursive binary lookup that returns the position of the first occurrence of the value * * @param array * Sorted array * @param start * Start position * @param end * End position * @param findvalue * The value to be found * @return The position of the value in the array, starting with 0. Cannot find return-1 */public static int searchrecursive (int[] array, int start, int end,int findvalue) {///If the array is empty, return 1 directly, that is, find failed if (ARRA y = = null) {return-1;} Count++;if (start <= end) {//middle position int middle = (start + end)/1;//median int middlevalue = array[middle];if (Findvalue = = Middlevalue) {//Equals middle value returns return middle directly,} else if (Findvalue < Middlevalue) {///less than median value in front of median value find return searchrecursive (array , start, middle-1, findValue);} else {//greater than median after median value find return searchrecursive (array, middle + 1, end, findvalue);}} else {//return-1, that is, find failed return-1;}} /** * Loop binary lookup, returns the position of the first occurrence of the value * * @param array * Sorted array * @param findvalue * Required value * @return value in the position of the array, from 0 start. Cannot find return-1 */public static int searchloop (int[] array, int findvalue) {//If the array is empty, return 1 directly, that is, find failed if (array = = null) {return-1;} Start position int start = 0;//end position int end = Array.length-1;while (start <= end) {count++;//middle position int middle = (start + end)  /2;//middle value int middlevalue = array[middle];if (findvalue = = Middlevalue) {//equals median returns return middle;} else if (Findvalue < Middlevalue) {///less than median value is found in front of the median value, end = Middle-1;} else {//greater than median is found after the median value start = middle + 1;}} Returns-1, which is the lookup failed return-1;}}

  

Java implementation of binary search algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.