Java recursive implementation of the dichotomy, I hope you point out

Source: Internet
Author: User

/*3. Find the elements in an ordered array using the dichotomy method. The returned index is found, and no output-1 is present. Using recursion to implement   */  import java.util.scanner;  public class recursion{   public static void main (String[]args)   {  system.out.println (" Please enter the integer to be searched for ");   scanner sc=new scanner (system.in);  int num =  Sc.nextint ();   int []a={1,2,3,4,5,6,7,8,9};      int front  = 0;      int end = a.length-1;  int  mid =  (front+end)/2;            // Recursive implementation of dichotomy  int index=findarray (A,front,mid,end,num);   if (index!=-1)   {   System.out.println ("Find the number" +index);   }  else  {  system.out.println ( index);   } }  public static int findarray (int[]b,int front,int& NBsP;mid,int end,int num)  {int index = -1; if (front==end)  {if (b[mid]==num ) {Return mid;} Else{return index;}  }    else if (front<end)   { if (b[mid]==num) {index =  Mid;return index;} Else if (B[mid]>num) {end = mid-1;mid =  (front+end)/2;return findarray (B,front, Mid,end,num);} Else if (b[mid]<num) {front=mid+1;mid= (front+end)/2;    return findarray (b, Front,mid,end,num);  }     } //why the following else needs to be commented out, without commenting out the compilation will be an error saying the missing return value     else   {   return index;    }  //   return index;   }  }

Why the program error, said else statement after the missing return value

This article is from the "11890208" blog, please be sure to keep this source http://11900208.blog.51cto.com/11890208/1865846

Java recursive implementation of the dichotomy, I hope you point out

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.