Searching Algorithms

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Searching

Algorithms
Data Structures

Turkan Ahmed
Computer Eng. Dept. , Collage of Eng., University of Mosul
Introduction to Search Algorithms
2

 A search algorithm is a method of locating a specific item


of information in a larger collection of data. This section
discusses two algorithms for searching the contents of an
array.
 Sequential (linear) search
 Binary search
Searching
3

A question you should always ask when


selecting a search algorithm is “How fast does
the search have to be?” The reason is that, in
general, the faster the algorithm is, the more
complex it is.

When there are a large number of items to


search, it is often necessary to have a strategy
for finding a specific item.
Sequential Search on an Unordered File
4

Basic algorithm:
Get the search criterion (key)
Get the first record from the file
While ( (record != key) and (still more records) )
Get the next record
End_while

 When do we know that there wasn’t a record in the


file that matched the key?
Sequential Search on an Ordered File
5

 Basic algorithm:
Get the search criterion (key)
Get the first record from the file
While ( (record < key) and (still more records) )
Get the next record
End_while
If ( record = key )
Then success
Else there is no match in the file
End_else
 When do we know that there wasn’t a record in the
file that matched the key?
Linear Search
6

 A linear search looks through list from the


beginning until it encounters the desired item.
 If the list is sorted, the search process can end
when a value greater than the one sought out is
found.
 This is a very simple algorithm.

 It uses a loop to sequentially step through an

array, starting with the first element.


 It compares each element with the value being
searched for and stops when that value is found
or the end of the array is reached.
Linear Search
7

int foundIt = -1;


for (int i = 0; i < arraylength; i++)
{
if (array[i] == searchValue)
{
foundIt = i;
break; // out of for loop
}
} // at this point, foundIt holds position of item or –1
Efficiency of the Linear Search
8

 The advantage is its simplicity.


 Itis easy to understand
 Easy to implement

 Does not require the array to be in order

 The disadvantage is its inefficiency


 Ifthere are 20,000 items in the array and what you are
looking for is in the 19,999th element, you need to search
through the entire list.
SEARCH CLASS IMPLEMENTATION
9

// compare key to every element of array until location is


// found or until end of array is reached; return subscript of
// element if key or -1 if key not found
int SEARCH:: linearSearch( const int array[], int key, int
sizeOfArray )
{
for ( int j = 0; j < sizeOfArray; j++ )
if ( array[ j ] == key ) // if found,
return j; // return location of key
return -1; // key not found
} // end function linearSearch
Binary Search “Divide and Conquer”
10

 If we have an ordered list and we know


how many things are in the list (i.e.,
number of records in a file), we can use
a different strategy.
 The binary search gets its name because

the algorithm continually divides the list


into two parts
Binary Search “Divide and Conquer”
11

 The binary search is much more efficient than the


linear search.
 It requires the list to be in order.

 The algorithm starts searching with the middle


element.
 If the item is less than the middle element, it starts
over searching the first half of the list.
 If the item is greater than the middle element, the
search starts over starting with the middle
element in the second half of the list.
 It then continues halving the list until the item is
found.
How a Binary Search Works
12

Always look at the center


value. Each time you get to
discard half of the remaining
list.

Is this fast ?
Binary Search
13

 A binary search assumes you start with a sorted list.


 Algorithm:
 The list is divided in half to find the mid-point.

 At this mid-point, one of three states exist:


1. You have found the item.
2. The item is less than the mid-point.
3. The item is greater than the mid-point.
 If the value sought is not found, divide the remaining
space in half and find a new mid-point.
 Repeat until you find the item or run out of space to
divide.
SEARCH CLASS IMPLEMENTATION
14

// Binary search on the data


long BinarySearch::binarySearch(long data[], long
searchElement )
{
long low = 0; // low end of the search area
long high = size - 1; // high end of the search area
long middle = ( low + high + 1 ) / 2; // middle element
int location = -1; // return value; -1 if not found
SEARCH CLASS IMPLEMENTATION
15

do // loop to search for element


{
for ( int i = 0; i < middle; i++ )
// if the element is found at the middle
if ( searchElement == data[ middle ] )
location = middle; // location is the current middle
else if ( searchElement < data[ middle ] )
// middle is too high
high = middle - 1; // eliminate the higher half
else // middle element is too low
low = middle + 1; // eliminate the lower half
SEARCH CLASS IMPLEMENTATION
16

middle = ( low + high + 1 ) / 2; // recalculate the


middle
} while ( ( low <= high ) && ( location == -1 ) );

return location; // return location of search key


} // end function binarySearch
return location; // return location of search key

} // end function binarySearch


Efficiency of the Binary Search
17

• Much more efficient than the linear search.

Binary search is O(logn) algorithm

You might also like