In linear search method a loop is run in th array and it checks the elements.
In Binary search method it starts with an interval covering the whole array.
Array is divided in two halves. If the search element is less than the element
in the middle the array should be shorted to the lower half. If the element
is greater than middle than array is narrowed to the upper half.
Array should be in sorted order. It is a limitation.
It is a fast search method.
References: https://www.youtube.com/watch?v=1HIFzve0zCM&t=334s
Q2. Modify the code in Example 2.4 so that it can take user input for the
following
// orderedArray.java
////////////////////////////////////////////////////////////////
import java.util.Scanner;
classOrdArray
{
private long[] a; // ref to array a
//-----------------------------------------------------------
nElems = 0;
//-----------------------------------------------------------
return nElems;
//-----------------------------------------------------------
int lowerBound = 0;
intcurIn;
while(true)
if(a[curIn]==searchKey)
return curIn; // found it
else if(lowerBound>upperBound)
if (a[curIn] <searchKey)
else
} // end while
} // end find()
//-----------------------------------------------------------
int j;
break;
a[k] = a[k-1];
//-----------------------------------------------------------
int j = find(value);
return false;
else // found it
a[k] = a[k+1];
return true;
} // end delete()
//-----------------------------------------------------------
System.out.println("");
//-----------------------------------------------------------
} // end class OrdArray
////////////////////////////////////////////////////////////////
classOrderedApp
//-----------------------------------------------------------
//-----------------------------------------------------------
long value;
//-----------------------------------------------------------
System.out.print("The numbers are:");
//-----------------------------------------------------------
if(arr.find(searchKey) = arr.size() )
else
//-----------------------------------------------------------
//-----------------------------------------------------------
} // end main()
References:
Robert ford, Arrays in Data Structures and Algorithm in Java,800 East 96th Street,
Indianapolis, Indiana 46240
Middle number
Position
Lower Upper Position
2ndHalf
References https://www.youtube.com/watch?v=1HIFzve0zCM&t=334s