searching - Princeton High School

advertisement
SEARCHING
Most basic search:
Linear or Sequential search.
We look at each element in the array until we find the
element or we reach the end of the array.
//********************************************************************
// Guests.java
Author: Lewis/Loftus/Cocking
//
// Demonstrates a linear search.
//********************************************************************
import cs1.Keyboard;
public class Guests
{
//----------------------------------------------------------------// Creates an array of guests' names and allows the user to
// search the array for a particular name.
//----------------------------------------------------------------public static void main (String[] args)
{
String[] guests = {"Paul", "Wendy", "Jared", "Eric", "Ayame",
"Ian", "Isobel", "Hakem"}; The boolean variable found is used
String name;
to record whether the name was
boolean found = false;
found or not.
System.out.print ("Enter a name: ");
name = Keyboard.readString();
The for loop goes through all
// Perform a linear search
the elements of the array.
for (int index = 0; index < guests.length; index++)
if (name.equals(guests[index]))
found = true;
The equals method is used
to compare the strings.
if (found)
System.out.println (name + " is on the guest list.");
else
System.out.println (name + " is not on the guest list.");
}
}
You may need to search an array in more than one
place in a program.
//********************************************************************
// Searches.java
Author: Lewis/Loftus/Cocking
//
// Demonstrates the linear and binary search algorithms.
//********************************************************************
The linearsearch method
returns the index at which
public class Searches
the element was found or -1
{
if the element was not found
//----------------------------------------------------------------// Searches the array of integers for the specified element using
// a linear search. The index where the element was found is
// returned, or -1 if the element is not found.
//----------------------------------------------------------------public static int linearSearch (int[] numbers, int key)
{
for (int index = 0; index < numbers.length; index++)
if (key == numbers[index])
return index;
Once the element is found,
return -1;
there is no need to search
}
-1 is not a valid index
further.
If the data is stored in order, we can
use a binary search
Smallest to largest.
//----------------------------------------------------------------// Searches the array of integers for the specified element using
// a binary search. The index where the element was found is
// returned, or -1 if the element is not found.
// NOTE: The array must be sorted!
//----------------------------------------------------------------public static int binarySearch (int[] numbers, int key)
{
int low = 0, high = numbers.length-1, middle = (low + high) / 2;
Start with the middle element
If high is greater than low, the key
element is not in the array.
while (numbers[middle] != key && low <= high)
{
The key element is in the lower half of
if (key < numbers[middle])
the current portion of the array.
high = middle - 1;
else
low = middle + 1;
The key element is in the upper half of
middle = (low + high) / 2;
the current portion of the array
}
if (numbers[middle] == key)
return middle;
If the key element is found, it
else
will be in the middle. If it is not
return -1;
found, it will return -1.
}
}
In the binary search, low and high keep track
of the part of the array to be searched.
If the key element is less than the element in
the middle, then we know it must be in the
lower half of the array eliminating all those
elements in the upper half of the array.
If the key element is greater than the element
in the middle, then we know it must be in the
upper half of the array (if it is in the array at
all)
Download