Вы находитесь на странице: 1из 36

SFG

Sal F. Gambino
CS-342

C++ CS 342

Data Structures
&
Linear & Binary searches
1
SFG
SEARCHING Sal F. Gambino
CS-342

The Process Of Looking For A Specific


Value
(Also Called Key)
In A List Is An Important Task In Problem
Solving.
List May Be Ordered Or Unordered.
2
SFG
UNORDERED LISTS Sal F. Gambino
CS-342

• unordered lists are searched by


comparing the value of each index
in the list against the key value.
•if a match is found,
then the key is located at that index;
•otherwise:
key is not in the list.

3
SFG
Requirements for a Linear Search Sal F. Gambino
of an unorderedList CS-342

1. the list structure


( 1-Dim array )
2. the actual SIZE of the list
(integer)
3. the key being searched for
(target)
4. a flag to indicate search result
(boolean)
5. the location to indicate where the key was
found
(index )
4
O(n+1)/2 if Found; Average case SFG
Sal F. Gambino
KEY 9 9 9 9 CS-342

list[0] 20 20 20 20
list[1] 29 29 29 29
list[2] 12 12 12 12
list[3] 9 9 9 9
: : : :

list 24 24 24 24
[LIST_SIZE-1]

COMPARE UNTIL KEY IS FOUND OR


5
O(n) if NOT found; Worst-case SFG
Sal F. Gambino
KEY 13 13 13 13 13
CS-342

list[0] 20 20 20 20 20
list[1] 29 29 29 29 29
list[2] 12 12 12 12 12
list[3] 9 9 9 9 9
: : : : .. :

list 24 24 24 24 24
[LIST_SIZE-1]

NO MORE VALUES IN LIST and KEY is NOT FOUND


6
SFG
void SeqSearch Sal F. Gambino
( const List_Type list, // to be searched CS-342
Component_Type key, // being searched for
int size, // of the list
int &index, // of value if found
int &found // 1 if found, 0 otherwise
)

•If key is matched, found is set to 1 and index


returns the location.
•Otherwise, found is 0 and index returns size.
•Searching stops when the key is found or
when there are no more values to check.

7
SFG
Sal F. Gambino
{ // SeqSearch() CS-342
found = 0;
index = 0;
while ( (index < size) && ( !found ) )
{
if (key == list[index])
found = 1;
else
index ++;
}
return;
} // end of SeqSearch()
8
SEQUENTIAL SEARCH IN AN
SFG
ORDERED LIST Sal F. Gambino
CS-342

WHEN SEARCHING FOR A KEY IN


AN ORDERED LIST,
IT IS POSSIBLE TO KNOW THAT
THE VALUE IS NOT IN THE LIST
AFTER PASSING THE INDEX
WHERE THE ITEM WOULD BE AT
IF IT WERE IN THE LIST.

9
SEQUENTIAL SEARCH IN AN SFG
ORDERED LIST
Sal F. Gambino
KEY 30 30 30 30 CS-342

list[0] 9 9 9 9
list[1] 12 12 12 12
list[2] 15 15 15 15
list[3] 30 30 30 30
: : : :

list 74 74 74 74
[LIST_SIZE-1]
STOP when KEY = VALUE ON LIST or
10
SEQUENTIAL SEARCH IN AN SFG
ORDERED LIST
Sal F. Gambino
KEY 20 20 20 20 CS-342

list[0] 9 9 9 9
list[1] 12 12 12 12
list[2] 15 15 15 15
list[3] 30 30 30 30
: : : :

list 74 74 74 74
[LIST_SIZE-1]
STOP when KEY < VALUE ON LIST.
11
SEQUENTIAL SEARCH IN AN
SFG
ORDERED LIST Sal F. Gambino
CS-342

void OrdSearch
( const List_Type list, // to be searched
Component_Type key, // being searched for
int size, // of the list
int &index, // of value if found
int &found // 1 if found,
) // 0 otherwise

12
{ // OrdSearch()
index = 0;
SFG
int stop = 0; Sal F. Gambino

list[size] = key; CS-342

// Stop when value is found or not in list


while ( !stop )
if ( key > list[ index ] )
index ++;
else
stop = 1;
if ( index != size && key == list[ index ] )
found = 1;
else
found = 0;
return;
} // OrdSearch() 13
Timings for Serial Search
SFG
Worst-case time for serial search:
Sal F. Gambino
•For an array of N elements, the worst-case
CS-342
time to find an element is
n array accesses
Average-case time for serial search:
•For an array of N elements, the average
case time to find an element is
(n + 1 )/2 array accesses
Best-case time for serial search:
•For an array of N elements, the best-case
time is just ONE array access
Note: unless the best case has a high
probability of occurring,
usually is not used in an analysis 14
SFG
MORE ON SEARCHING Sal F. Gambino
CS-342

SEARCHING AN ORDERED LIST


CAN BE FASTER THAN SEARCHING AN
UNORDERED LIST
(NOT SO WHEN THE KEY IS NOT IN THE LIST.)
SEQUENTIAL SEARCH OF
ORDERED AND UNORDERED LISTS
CAN BE INEFFICIENT WITH LARGE LISTS.

15
SFG
BINARY SEARCH IN AN Sal F. Gambino
ORDERED LIST CS-342

•Maybe used only if the array is sorted


•Implementation:
•Recursive (text page 564)
•Iterative
•Running time
•Worst-case and Average-case O(log2 n)

16
BINARY SEARCH SFG
Approximation formula
Sal F. Gambino

Logs in base 2 CS-342

• log2 8 = 3 23=8
• log2 32 = 5 25=32
• log2 1000 ~ 9 29~1000
closest integer
• log2 1024 = 10 210=1024

log2 n ~ nc used to approximate the


number of Compares
n = # of elements it takes to find an element
nc = # of Compares for the worst case
17
SFG
BINARY SEARCH IN AN
Sal F. Gambino
ORDERED LIST CS-342

Given a list with n values,


compare the key with the value at the middle.
1. if a match is found
stop the search.
2. if middle < key,
then the key (if there)
is in the bottom half of the list.
3. if middle > key,
then the key (if there)
is in the top half of the list.
18
this involves dividing the list by 2
SFG
Sal F. Gambino
(hence the name binary search)
CS-342
and determining if the key is in
the middle,
the upper,
or lower
half of the list.
this process is done repeatedly
until
the key is found
or
it is determined
that it is not in the list.
19
Binary Search
Middle = ( top + bottom ) / 2;
SFG
Middle = ( 0 + 7 )/2=3 Sal F. Gambino
CS-342

EXAMPLE #1:
93
key

list [0] 52 COMPARE


list [1] 62
list [2] 70
list [3] 90 middle
list [4] 91 top = middle +1
list [5] 93 top = 4
list [6] 124
list [7] 138
list[middle] < key 20
Binary Search
Middle = ( top + bottom ) / 2;
SFG
Middle = ( 0 + 7 )/2=3 Sal F. Gambino
CS-342

EXAMPLE #1:
93
key

list [0] 52 COMPARE


list [1] 62
list [2] 70
list [3] 90 middle
list [4] 91 top = middle+1
list [5] 93 top = 4
list [6] 124
list [7] 138
list[middle] < key 21
Binary Search
Middle = ( top + bottom ) / 2;
SFG
Middle = ( 4 + 7 )/2=5 Sal F. Gambino
CS-342
EXAMPLE #1:
key 93

COMPARE

list [4] 91 It took 2


list [5] 93 middle COMPARES
list [6] 124
list [7] 138
list[middle] = key FOUND! 22
Binary Search SFG
Middle = ( top + bottom ) / 2;
Middle = ( 0 + 7 )/2=3 Sal F. Gambino
CS-342

EXAMPLE #2:
68
key

list [0] 52 COMPARE


list [1] 62
list [2] 70
list [3] 90 middle
list [4] 91
list [5] 93 bottom = middle - 1
list [6] 124 bottom = 2
list [7] 138
list[middle] > key 23
Binary Search SFG
Middle = ( top + bottom ) / 2;
Middle = ( 0 + 7 )/2=3 Sal F. Gambino
CS-342

EXAMPLE #2:
68
key

list [0] 52 COMPARE


list [1] 62
list [2] 70
list [3] 90 middle
list [4] 91
list [5] 93
list [6] 124
list [7] 138
list[middle] > key 24
Binary Search
Middle = ( top + bottom ) / 2;
SFG
Middle = ( 0 + 2 )/2=1 Sal F. Gambino
CS-342

EXAMPLE #2:
68
key
COMPARE
list [0] 52
list [1] 62 middle
list [2] 70

top = middle +1
top = 2

list[middle] < key


25
Binary Search SFG
Middle = ( top + bottom ) / 2;
Sal F. Gambino
Middle = ( 2 + 2 )/2=2
CS-342

EXAMPLE #2:
68
key n = 8, nc = 3
COMPARE log2 8 = 3
top = bottom
It took 3
middle COMPARES
list [2] 70 Worst-case

list [middle] < > key, THEREFORE key IS NOT IN list


26
Binary Search
Middle = ( top + bottom ) / 2;
SFG
Middle = ( 0 + 7 )/2=3 Sal F. Gambino
CS-342

EXAMPLE #3:
200
key

list [0] 52 COMPARE


list [1] 62
list [2] 70
list [3] 90 middle
list [4] 91 top = middle +1
list [5] 93 top = 4
list [6] 124
list [7] 138
list[middle] < key 27
Binary Search
Middle = ( top + bottom ) / 2;
SFG
Middle = ( 0 + 7 )/2=3 Sal F. Gambino
CS-342

EXAMPLE #3:
200
key

list [0] 52 COMPARE


list [1] 62
list [2] 70
list [3] 90 middle
list [4] 91 top = middle+1
list [5] 93 top = 4
list [6] 124
list [7] 138
list[middle] < key 28
Binary Search
Middle = ( top + bottom ) / 2;
SFG
Middle = ( 4 + 7 )/2=5 Sal F. Gambino
CS-342
EXAMPLE #3:
key 200

COMPARE

list [4] 91
list [5] 93 middle top = middle +1
list [6] 124 top = 6
list [7] 138
list[middle] < key 29
Binary Search
Middle = ( top + bottom ) / 2;
SFG
Middle = ( 4 + 7 )/2=5 Sal F. Gambino
CS-342
EXAMPLE #3:
key 200

COMPARE

list [4] 91
list [5] 93 middle top = middle +1
list [6] 124 top = 6
list [7] 138
list[middle] < key 30
Binary Search
Middle = ( top + bottom ) / 2;
SFG
Middle = ( 6 + 7 )/2=6 Sal F. Gambino
CS-342
EXAMPLE #3:
key 200

COMPARE

top = middle +1
list [6] 124 middle top = 7
list [7] 138
list[middle] < key
31
Binary Search
Middle = ( top + bottom ) / 2;
SFG
Middle = ( 7 + 7 )/2=7 Sal F. Gambino
CS-342
EXAMPLE #3:
key 200
n = 8, nc = 3
log2 8 = 3
COMPARE
top = bottom However
It took 4
COMPARES
Worst-case
list [7] 138 middle the formula
approximates

list[middle]<>key,THEREFORE key IS NOT IN list 32


Binary Search
SFG
function header Sal F. Gambino
CS-342

void BinSearch
(const List_Type list, // to be searched
Component_Type key, // being searched for
int size, // of the list
int &index, // of value if found
int &found // 1 if found,
) // 0 otherwise

33
Binary Search
SFG
function Sal F. Gambino
CS-342

{ /*------------------------------------------------------------------
BinSearch()
If key is matched, found is set to 1 and index
returns the location.
Otherwise, found is 0 and index is set to -1.
------------------------------------------------------------------
*/
int top = 0; // first component
int bottom = size - 1; // last component
int middle; // middle component
found = 0;
34
SFG
while ( ( top <= bottom) && ( !found ) )
{ Sal F. Gambino

middle = (top + bottom) / 2; CS-342

if (list[middle] == key)
found = 1;
else if (list[middle] < key)
top = middle + 1;
else
bottom = middle - 1;
}

if (found)
index = middle;
else
index = -1;
}
35
SFG
Summary of searches Sal F. Gambino
CS-342

•Serial Search is quick to program but requires


linear time to find an item in both worst case and
average case
•Binary Search works well on a sorted array of items,
requiring O(log n) time in both the worst case and
the average case.
However,
as items are added or deleted, keeping the array
sorted may take considerable time.
Linear time for each insertion and deletion in the
worst case.
36

Вам также может понравиться