Академический Документы
Профессиональный Документы
Культура Документы
Sal F. Gambino
CS-342
C++ CS 342
Data Structures
&
Linear & Binary searches
1
SFG
SEARCHING Sal F. Gambino
CS-342
3
SFG
Requirements for a Linear Search Sal F. Gambino
of an unorderedList 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]
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]
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
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
15
SFG
BINARY SEARCH IN AN Sal F. Gambino
ORDERED LIST CS-342
16
BINARY SEARCH SFG
Approximation formula
Sal F. Gambino
• log2 8 = 3 23=8
• log2 32 = 5 25=32
• log2 1000 ~ 9 29~1000
closest integer
• log2 1024 = 10 210=1024
EXAMPLE #1:
93
key
EXAMPLE #1:
93
key
COMPARE
EXAMPLE #2:
68
key
EXAMPLE #2:
68
key
EXAMPLE #2:
68
key
COMPARE
list [0] 52
list [1] 62 middle
list [2] 70
top = middle +1
top = 2
EXAMPLE #2:
68
key n = 8, nc = 3
COMPARE log2 8 = 3
top = bottom
It took 3
middle COMPARES
list [2] 70 Worst-case
EXAMPLE #3:
200
key
EXAMPLE #3:
200
key
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
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
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