import [Link].
Scanner;
public class find
{
public static void main(String args[])
{
int c, first, last, middle, n, find, array[];
Scanner in = new Scanner([Link]);
[Link]("Enter number of elements");
n = [Link]();
array = new int[n];
int count = 0;
int count1 = 0;
[Link]("Enter " + n + " integers");
int i = 0;
boolean found = false;
// set the boolean value to false until the key is found
for (c = 0; c < n; c++)
array[c] = [Link]();
[Link]("Enter value to find");
find = [Link]();
for ( i = 0; i < [Link]; i++)
{count ++;
if (array[ i ] == find)
{
found = true;
break;
}
}
if (found) //When found is true, the index of the location of key will be printed.
{
[Link]("Found " + find + " at location " + (i+1) + ".");
[Link]("The sequantial find found the number after " + count +
" comparisons.\n");
}
else
{
[Link](find + " not found in this array.");
}
first = 0;
last = n - 1;
middle = (first + last)/2;
while( first <= last )
{
if ( array[middle] < find ){
first = middle + 1;
count1++;
}
else if ( array[middle] == find )
{
count1++;
[Link](find + " found at location " + (middle + 1) + ".");
[Link]("The Binary find find found the number after " + count1 +
" comparisons.");
break;
}
else
last = middle - 1;
count1++;
middle = (first + last)/2;
}
if ( first > last )
{
[Link](find + " not found in the array.");
count1++;
}
}
}