CMP 202 (Searching)
CMP 202 (Searching)
SEARCHING
1
CONTENTS
✓ What is Searching?
✓ Types of Searching
▪ Linear Search
▪ Binary Search
2
WHAT IS SEARCHING?
3
SEARCHING
❖ We know that today’s computers store a lot of information.
❖ To retrieve this information proficiently we need a very efficient way of
doing it and to make sure we are retrieving the right information.
7
LINEAR SEARCH
✓ Linear or Sequential Search is the simplest of search algorithms.
✓ Linear Search has no pre-requisites for the state of the underlying data
structure.
data structure until either the element is found or the end of the structure is
reached.
✓ If the element is found, we usually just return its position in the data
structure.
9
LINEAR SEARCH (Example)
public static int linearSearch(int arr[], int elementToSearch) {
if (arr[index] == elementToSearch)
return index;
return -1;
}
10
LINEAR SEARCH (Example)...
To test it, we'll use a simple Array of integers: Here, we are searching for “67” in the below array
89 57 91 47 95 3 27 22 67 99
✓ Phonebook Search: Linear search can be used to search through a phonebook to find a
person’s name, given their phone number.
✓ Spell Checkers: The algorithm compares each word in the document to a dictionary of
correctly spelled words until a match is found.
✓ Finding Minimum and Maximum Values: Linear search can be used to find the minimum
and maximum values in an array or list.
✓ Searching through unsorted data: Linear search is useful for searching through unsorted
data.
14
ADVANTAGES OF LINEAR SEARCH
✓ Simplicity: Linear search is a very simple algorithm to understand and
implement.
✓ Works with unsorted data: Linear search works well with unsorted data. It
does not require any pre-processing or sorting of the data before
performing the search.
✓ Low memory usage: Linear search only requires a small amount of memory
to store the index of the current element being searched.
✓ Easy to debug: Because linear search is a simple algorithm, it is easy to
debug and troubleshoot any issues that may arise.
15
DISADVANTAGES OF LINEAR SEARCH
✓ Inefficient for large datasets: As the size of the dataset grows, the time
taken by linear search also increases proportionally.
✓ Limited applicability: Linear search is only suitable for datasets that are not
too large or not too complex.
✓ No early termination: Linear search does not have a mechanism to
terminate early once the target element is found.
✓ Inefficient for sorted data: When the data is already sorted, linear search is
not efficient because it needs to check each element one by one, even if
it has already passed the target element.
16
BINARY SEARCH
17
BINARY SEARCH
❖ Binary or Logarithmic Search is one of the most commonly used search
algorithms primarily due to its quick search time.
❖ This kind of search uses the Divide and Conquer methodology and requires
the data set to be sorted beforehand.
❖ If data is not sorted, then it to be sorted or we use Linear Search
❖ It divides the input collection into equal halves, and with each iteration
compares the target element with the element in the middle.
18
BINARY SEARCH (CONT)....
❖ If the element is found, the search ends.
❖ Else, we continue looking for the element by dividing and selecting the
appropriate partition of the array, based on if the target element is
smaller or bigger than the middle element.
19
BINARY SEARCH (CONT)....
Below are the steps:
✓ Else If x is greater than the mid element, then x can only lie in the right half
subarray after the mid element. So we recur for right half.
20
BINARY SEARCH (Example)...
Let’s use the below array
3 11 21 29 41 54 61 78 110 127
54 61 78 110 127
21
BINARY SEARCH (Example)...
public static int binarySearch(int arr[], int firstElement, int lastElement,
int elementToSearch) {
// termination condition
if (arr[mid] == elementToSearch)
return mid;
3 22 27 47 57 67 89 91 95 99
22
BINARY SEARCH (Example)...
// if the middle element is bigger than the goal element
return -1;
Output:
} 67 found at index: 5
We can use this algorithm like this:
int index = binarySearch(new int[]{3, 22, 27, 47, 57, 67, 89, 91, 95, 99}
23
ADVANTAGES OF BINARY SEARCH
24
DISADVANTAGES OF BINARY SEARCH
✓ Small unsorted arrays would take considerate time in sorting and then
searching the desired element.
25
APPLICATIONS OF BINARY SEARCH
❖ It is the most commonly used search algorithm in most of the libraries for searching.
❖ The Binary Search tree is used by many data structures as well which store sorted data.
26