Topic 8 - Searching Techniques
Topic 8 - Searching Techniques
Topic 8 - Searching Techniques
SEARCHING
TECHNIQUES
Sequential Search on an Unordered File
Sequential Search on an Ordered File
OUTLINE Linear Search
Binary Search
Interpolation Search
There are some very common
problems that we use computers to
solve:
- P l a c i n g r e c o r d s i n o r d e r, w h i c h w e
PROBLEMS call sorting
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
• 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?
Sequential Search of
Ordered vs. Unordered List
• Let’s do a comparison.
• If the order was ascending alphabetical on customer’s last names, how would the search for
John Adams on the ordered list compare with the search on the unordered list?
o Unordered list
–if John Adams was in the list?
–if John Adams was not in the list?
o Ordered list
–if John Adams was in the list?
–if John Adams was not in the list?
Ordered vs Unordered (con’t)
• Observation: the search is faster on an ordered list only when the item
being searched for is not in the list.
• Also, keep in mind that the list has to first be placed in order for the
ordered search.
• Conclusion: the efficiency of these algorithms is roughly the same.
• So, if we need a faster search, we need a completely different algorithm.
• How else could we search an ordered file?
Linear Search
Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit
Pseudocode
end procedure
Binary Search
We change our low to mid + 1 and find the new mid value again.
low = mid + 1
mid = low + (high - low) / 2
How a Binary Search Works (con’t)
Our new mid is 7 now. We compare the value stored at location 7 with
our target value 31.
The value stored at location 7 is not a match, rather it is more than what we
are looking for. So, the value must be in the lower part from this location.
How a Binary Search Works (con’t)
Hence, we calculate the mid again. This time it is 5.
We compare the value stored at location 5 with our target value. We find
that it is a match.
• 32 = 25 and 512 = 29
• 8 < 11 < 16 23 < 11 < 24
• 128 < 250 < 256 27 < 250 < 28
A Very Fast Algorithm!
• How long (worst case) will it take to find an
item in a list 30,000 items long?
210 = 1024 213 = 8192
211 = 2048 214 = 16384
212 = 4096 215 = 32768
Set lowerBound = 0
Pseudocode Set upperBound = n
if A[midPoint] = x
EXIT: x found at location midpoint
if A[midPoint] < x
set lowerBound = midPoint + 1
if A[midPoint] > x
set upperBound = midPoint - 1
end procedure
Interpolation Search
• In binary search, if the desired data is not found then the rest
of the list is divided in two parts, lower and higher. The
search is carried out in either of them.
Positioning Probing in Interpolation
Search
• If the middle item is greater than the item, then the probe position is
again calculated in the sub-array to the right of the middle item.
Otherwise, the item is searched in the subarray to the left of the middle
item. This process continues on the sub-array as well until the size of
subarray reduces to zero.
Procedure Interpolation_Search()
Set Lo → 0
Set Mid → -1
Set Hi → N-1
if A[Mid] = X
EXIT: Success, Target found at Mid
else
if A[Mid] < X
Set Lo to Mid+1
else if A[Mid] > X
Set Hi to Mid-1
end if
end if
End While
End Procedure
Questions?