Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 14

Chapter 6: Searching

Introduction
One of the most important operation on
data processing is the searching.
Searching is the process of finding the
location of a target item in a list.
Search algorithm is used for:
Determine whether a particular item is in the
list.
If the data are specially organized, find the
location where a new item can be inserted.
Find the location of an item to be deleted.
Introduction
The search algorithm’s performance is
crucial because:
For accessing information quickly.
If the search is slow, it takes a large
amount of computer time to accomplish
the task.
A searchValue is used to facilitate
searching.
Linear Sequential Search
The simplest form of search.
Items in a list are searched one by one
starting from the first and compared to a
given search value.
The search ends when the search value
matches with the given item.
This technique generally works well for a
small list or when the list is accessed
infrequently.
Linear Sequential Search Algorithm
Consider array A with n elements and
the search value. The algorithm:
1. Compare the search value with the first
element in array. If match is found, then
search is successful.
2. If no match is found, try to match the
next element in the array.
3. Repeat step 2 until a match is found.
Binary Search
The binary search algorithm searches
the list by first comparing search value
with the middle element in the list.
If the search value is not the same as
the middle element, it determines
whether the search value is in the
upper half (first half) or in the lower
half (last half) of the list.
Binary Search
If it is in the first half, then the last half can
be dropped.
Likewise, if the search value is in the last half,
the first half can be dropped.
The elimination of either the first half or the
last half depends on the value of the middle
element.
The process is continued until the target
elements is found or reduced search list
range becomes zero.
Binary Search Algorithm
Given the sorted array A with n
elements and the search value. The
algorithm:
1. Compare the search value with the
middle element in the array. The position
of the middle element is computed from
(first + last)/2. If the match is found, the
search is successful.
Binary Search Algorithm cont….
2. If no match is found, determine whether the
search value is less or more than the middle
value.
• 2.1 If the search value is less than the middle value,
search the sublist from the first element to the
element just before the middle value. (position
middle – 1)
• 2.2 If the search value is greater than the middle
value, search the sublist from position middle+1 to
the last position.
3. Repeat step 2 for smaller sublist until a
match is found.
Jump Search
Like Binary Search, Jump Search is a searching
algorithm for sorted arrays. The basic idea is to check
fewer elements (than linear search) by jumping ahead
by fixed steps or skipping some elements in place of
searching all elements.
For example, suppose we have an array arr[] of size n
and block (to be jumped) size m. Then we search at
the indexes arr[0], arr[m], arr[2m]…..arr[km] and so
on.
Once we find the interval (arr[km] < x < arr[(k+1)m]),
we perform a linear search operation from the index
km to find the element x.
Jump Search
Let’s consider the following array: (0, 1, 1, 2, 3, 5, 8,
13, 21, 34, 55, 89, 144, 233, 377, 610). Length of the
array is 16. Jump search will find the value of 55 with
the following steps assuming that the block size to be
jumped is 4.
STEP 1: Jump from index 0 to index 4;
STEP 2: Jump from index 4 to index 8;
STEP 3: Jump from index 8 to index 16;
STEP 4: Since the element at index 16 is greater than
55 we will jump back a step to come to index 9.
STEP 5: Perform linear search from index 9 to get the
element 55.
Jump Search
What is the optimal block size to be skipped?
In the worst case, we have to do n/m jumps and if the
last checked value is greater than the element to be
searched for, we perform m-1 comparisons more for
linear search. Therefore the total number of
comparisons in the worst case will be ((n/m) + m-1).
The value of the function ((n/m) + m-1) will be
minimum when m = √n. Therefore, the best step size
is m = √n.
Exercise
Show the process of searching by using
the binary search for number 12: list
the index for first , last and middle
value in every search.

21, 45, 12, 65, 34


Exercise
Given the number 21, 45, 12, 65, 34

Show the process of searching by using the


binary search for number 45: list the index
for first , last and middle value in every
search.
Do the same process based on Jump search.

You might also like