Java Learning Note----binary Lookup recursion

Source: Internet
Author: User

Public class main{    public static int rank (int key,  Int[] a) {        return rank (key,a, 0 ,a.length  -1 );     }    public static int rank (int  key, int[] a, int lo , int hi) {         if  (lo > hi) {System.out.println ("  cannot find  "); return -1;}         system.out.println (string.format ("max index is  ->%s ", hi));        int mid = lo +  (hi)  - lo)/2;        system.out.println (String.format ("index  is -> %s ", mid));        if  (key <  a[mid]) { system.out.println (STRING.FOrmat ("%s is less than %s", key,a[mid]);  return rank (key, a, lo , mid-1); }        else if  (key > a[mid]) { System.out.println (string.format ("%s is more than %s", key,a[mid]);  return rank ( key,a, mid+1,hi);}         else {system.out.println (String.format ("%s matched  %s ----------> index %s!!! ", key,a[mid],mid)); return mid;}     }    public static void main (String args[]) {        int[] a = { 1,2,3,4,5,6,43534,534535,1231,313,53,5,21312,3,123,123,12321,312312,31,24,5,34,6,7,353,5423,42,424,234,1321,34,43,6457,4 } ;       rank (100,a);        rank (5 , a);     }}


Java Learning Note----binary Lookup recursion

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.