Binary Search Algorithm Java Real-Time Representation (original article: http://www.oschina.net/code/snippet_107039_5818)

Source: Internet
Author: User
Package search;/*** @ author Lei 2011-8-17 */public class binarysearch {/*** Binary Search ** note: binary Search only targets arrays or collections in an ordered manner ** @ Param target * @ Param array * @ return */static Boolean binarysearch (INT target, int [] array) {int front = 0; int tail = array. length-1; // determine whether the Sub-array can be split into two parts again while (front <= tail) {// obtain the middle position of the sub-array, perform binary int middle = (front + tail)/2 based on the intermediate position; if (array [Middle] = target) {return true ;} else if (array [Middle]> Target) {tail = middle-1 ;}else {front = middle + 1 ;}} return false ;} public static void main (string [] ARGs) {int [] array = new int [] {1, 2, 3, 5, 7, 9, 17,121,454 5}; system. out. println (binarysearch (4545, array ));}}

 

Related Article

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.