Transcript Slide 1

1
CS212: DATASTRUCTURES
Lecture 3: Searching
Lecture Contents
2

searching
 Sequential search algorithm.
 Binary search algorithm.
Search Algorithms
3


Searching , the process used to find the location of
a target among a list of objects.
In this chapter, we will study searches that work with
arrays
Search
Algorithms
Sequential
Search
Binary
Search
Search Algorithms
4
1.
Sequential search.
1.
It’s not requires an ordered list.
2.
Binary search.

It requires an ordered list.
1/ Sequential (Linear) Search
5



Search an array or list by checking items one at a time.
Sequential search is usually very simple to implement, and is
practical when the list has only a few elements, or when
performing a single search in an unordered list.
Look at every element : This is a very straightforward loop
comparing every element in the array with the target(key).

Eighter we find it,

or we reach the end of the list!
Locating data in unordered list.
6
Sequential Search Algorithm
7

The searching algorithm requires three parameters:
1.
The list.
2.
An index to the last element in the list.
3.
The target.
Sequential Search Algorithm
8
algorithm SeqSearch (val list <array>, val last <index>,
val target <keyType>)
Locate the target in an unordered list of size elements.
PRE list must contain at least one element.
last is index to last element in the list.
target contains the data to be located.
POST if found – matching index stored in Location.
if not found – (-1) stored in Location
RETURN Location<integer>
Sequential Search Algorithm
9
looker = 0
loop (looker < last AND target not equal list(looker))
looker = looker + 1
if (target == list[looker] )
Location= looker
else
Location = -1
End If
return Location
end SeqSearch
Recursive Sequential Search Algorithm
10
algorithm RecSeqsearch (val I <integer>), val J <integer>) , val
target<keyType>)
Locate the target in an unordered list of size elements.
PRE I is is index to first element in the list.
last is index to last element in the list.
target contains the data to be located.
POST if found – matching index stored in Location.
if not found – (-1) stored in Location
RETURN Location<integer>
Recursive Sequential Search Algorithm
11
if (A[I] == target)
location = I
else
if (I == J)
location = -1
else
location = RecSeqsearch(I+1,J, target)
End If
Return location
End RecSeqsearch
Recursive sequential search (2)
12
Algorithm sequentialSearch (item<integer>,list<array>, listSize<integer>)
Pre
item contains a value, listSize contains the actual size of the array list
Post
find the location of item in list
Return
either the item found and its location is returned or not and -1 returned
if (listSize == 0)
return -1
if (list[listSize-1] == item)
return listSize-1
else
return sequentialSearch (item, list, listSize-1)
End sequentialSearch
2/ Binary search algorithm
13


Search a sorted array by repeatedly dividing the
search interval in half.
A fast way to search a sorted array is to use a binary
search.
Binary search algorithm
14
Calculate the middle element
Test the data in the element at the middle of the array.
Target < middle element
Target > middle element
it is in the first half before middle
it is in the second half after middle
Calculate the middle element
Calculate the middle element
Test the data in the element at the
middle of the array.
Test the data in the element at the
middle of the array.
Target < middle
it is in the
first half!
Target > middle
it is in the
second half!
Target < middle
it is in the
first half!
Target > middle
it is in the
second half!
..
..
..
..
If the middle element equals to the Target , the algorithm stops
mid=(first+last)/2
target > A[mid]
first = mid +1
target < A[mid]
last = mid -1
target == A[mid]
15
target < A[mid]
last = mid -1
target > A[mid]
first = mid +1
target > A[mid]
first = mid +1
target < A[mid]
last = first  not found stop
16
Recursive Binary search algorithm
17
algorithm RecBinarySearch (val First<index>, val last
<index>,val target <keyType>)
Locate the target in an ordered list of size elements.
PRE list must contain at least one element.
First is index to first element in the list.
last is index to last element in the list.
target contains the data to be located.
POST if found – matching index stored in Location
if not found – (-1) stored in Location
RETURN Location<integer>
Recursive search algorithm
18
m := ( first  last ) / 2
if target = am then
base cases
Location= m
else if (first=last) then
Location= -1
else if (target < am) then
recursive calls
Location =binarySearch(first, m-1, target)
else if (target > am) then
Location=binarySearch(m+1, last, target )
Return Location
End RecBinarySearch
Example
19
Return 4
[0]
[1]
[2]
[3]
[4]
3
5
7
11
20
BinarySearch (0,4,20)
M=0+4/2=2
20 >7 then
binarySearch(3 , 4,20)
Recursive call
Return 4
BinarySearch (3,4,20)
M=3+4/2=3
20 >11 then
binarySearch(4 , 4,20)
Recursive call
Return 4
BinarySearch (4,4,20)
M=4+4/2=2
20 == 20
Binary search algorithm (iterative)
20
Algorithm BinarySearch (list<array>, key <integer>,
listSize<integer> )
Search an ordered list using Binary Search
PRE list must contain at least one element.
listSize is the actual size of the list.
key contains the data to be located.
POST if found – matching index stored in Location
if not found (-1) stored in Location
RETURN Location<integer>
Binary search in iterative algorithm
21
first=0
last=listSize-1
while (first<= last)
{ mid = ( first  last ) / 2
if ( list [mid] == key)
return mid
else if (list[mid] < key)
first = mid + 1
else
last = mid - 1
}
return -1
End BinarySearch
22
End Of Chapter
References:
•
Text book, chapter2: Searching