Algorithm Exercises
Package common;
Import Java.util.Arrays;
public class BS {
void nbs (int n,int s[],int x)
{
int low=0;
int high=n-1;
while (Low<=high)
{
int mid= (Low+high)/2;
if (X==s[mid])
{
System.out.print ("P:" +mid);
System.exit (0);
}
else if (X>s[mid])
{
low=mid+1;
}
Else
{
High=mid-1;
}
}
System.out.print ("Have no The" +x);
System.exit (1);//non-normal exit
}
void dbs (int s[],int low,int high,int x)
{
if (Low>high)
{
System.out.print ("Have no The" +x);
System.exit (1);
}
int mid= (Low+high)/2;
if (X==s[mid])
{
System.out.print ("P:" +mid);
System.exit (0);//Normal exit
}
else if (X>s[mid])
{
DBS (S,MID+1,HIGH,X);
}
Else
{
DBS (S,LOW,MID-1,X);
}
}
public static void Main (string[] args) {
TODO auto-generated Method Stub
BS a=new BS ();
int s[]={1,7,9,90,888,98,78,880};
Arrays.sort (s);
for (int i:s)
{System.out.println (i);}
SYSTEM.OUT.PRINTLN ("Non-recursive binary lookup:");
A.nbs (S.length, S, 1);
//********************************************************************
System.out.println ("Recursive two-point lookup:");
A.dbs (S, 0, S.length-1, 90);
//********************************************************************
[Pig]^ (* ̄ (oo)  ̄) ^: Because the above two-point lookup is not a return value, the end takes the Exit function,
Therefore, NBS and DBS cannot produce results at the same time (in order), because exit indicates that the JVM is stopped, so note
}
}
Two-point Search