Slide 6 - Searching Algorithms
Slide 6 - Searching Algorithms
Trong-Hop Do
University of Information Technology, HCM city
1
Searching Algorithms
2
Applications of searching
3
Types of searching algorithms
Searching Algorithms are designed to check for an element or retrieve an element from any data
structure where it is stored. Based on the type of search operation, these algorithms are generally
classified into two categories:
• Sequential Search: In this, the list or array is traversed sequentially and every element is checked.
For example: Linear Search.
• Interval Search: These algorithms are specifically designed for searching in sorted data-structures.
These type of searching algorithms are much more efficient than Linear Search as they repeatedly
target the center of the search structure and divide the search space in half. For Example: Binary
Search.
4
Linear search
• Given an array arr[] of n elements, write a function to search a given element x in arr[].
5
Linear search
Implement Linear search on an Array
6
Linear search
7
Linear search
8
Linear search improved version
• How to improve?
• Reduce the number of ending conditions to check in each loop to 1
9
Linear search improved version
• Idea
• Insert x at the the end of the array. The ending condition to check is if current data equals x.
• Algorithm:
i <- 0, A[n] = x
while A[i] x
i <- i+1
end while
10
Linear search improved version
int linearSearchA(int A[],int n,int x) {
int i = 0; A[n] = x;
while (A[i] != x)
i++;
if (i < n) return i;
else return -1;
}
11
Linear search improved version
Node* linearSearchA(List A, int x) {
Node *p = A.pHead, *t = new Node(x);
if (!t) throw "out of memory";
addTail(A, t);
while (p->info != x) p = p->pNext;
if (p == A.pTail) return p;
else return NULL;
}
12
Binary search
• Given a sorted array arr[] of n elements, write a function to search a given element x in arr[].
• A simple approach is to do linear search.The time complexity of above algorithm is O(n). Another
approach to perform the same task is using Binary Search.
13
Binary search
We basically ignore half of the elements just after one comparison.
1. Compare x with the middle element.
2. If x matches with middle element, we return the mid index.
3. Else If x is greater than the mid element, then x can only lie in right half subarray after the mid
element. So we recur for right half.
4. Else (x is smaller) recur for the left half.
14
Binary search
15
Binary search implementation on array
16
Binary search implementation on Linked List
To implement binary search on Linked List, we need a nother data structure: Binary Search Tree
17
Binary search
• Best case: x[(l+r) div 2]=x → run 1 time → O(1).
• Average: O(log(n))
18
Interpolation Search
• Given a sorted array of n uniformly distributed values arr[], write a function to search for a particular element x.
• Linear Search finds the element in O(n) time, Jump Search takes 𝑂( 𝑛) time and Binary Search take O(Log n) time.
• Binary Search always goes to the middle element to check. Interpolation search may go to different locations
19
Interpolation Search
20
Interpolation Search Algorithm
Step1: In a loop, calculate the value of “pos” using the probe position formula.
Step3: If the item is less than arr[pos], calculate the probe position of the left sub-
array. Otherwise calculate the same in the right sub-array.
21
Interpolation Search
22
Interpolation Search
23
Interpolation Search
24
Interpolation Search Implementation
int interpolationSearch(int arr[], int n, int x){
int lo = 0, hi = (n - 1);
while (lo <= hi && x >= arr[lo] && x <= arr[hi]) {
if (lo == hi){
if (arr[lo] == x) return lo;
return -1;
}
int pos = lo + (((double)(hi-lo) /
(arr[hi]-arr[lo]))*(x - arr[lo]));
if (arr[pos] == x) return pos;
if (arr[pos] < x)
lo = pos + 1;
else
hi = pos - 1;
}
return -1;
25
}
Interpolation Search
• Time Complexity:
• If elements are uniformly distributed, then O (log log n)).
• In worst case it can take upto O(n).
1 2 3 4 5 6 7 8 9 10000
26
Jump Search
27
Jump search
28
Jump search: Optimal Size of block size m
Hence, the total number of comparisons will be (𝑛/𝑚 + (𝑚 − 1)) . This expression
has to be minimum, so that we get the smallest value of m (block size).
𝑛 ′ 𝑛 𝑛
Optimal block size m: +𝑚−1 =− +1=0→ =1→𝑚= 𝑛
𝑚 𝑚2 𝑚2
29
Jump search
30
Jump search
31
Jump search
32
Jump search
33
Jump search
34
Jump search
Step 7: Compare A[10] with item. Since A[10] == item, index 10 is printed as the
valid location and the algorithm will terminate
35
Jump search
int jump_Search(int a[], int n, int item) {
int i = 0;
int m = sqrt(n); //initializing block size= √(n)
while(a[m] <= item && m < n) {
i = m; // shift the block
m += sqrt(n);
if(m > n - 1) // if m exceeds the array size
return -1;
}
for(int x = i; x<m; x++) { //linear search in current block
if(a[x] == item)
return x; //position of element being searched
}
return -1;
} 36
Jump search
Time Complexity:
• The while loop in the above C++ code executes n/m times because the loop counter
increments by m times in every iteration. Since the optimal value of m= √n , thus,
n/m=√n resulting in a time complexity of O(√n).
37
Jump search
Key Points to remember about Jump Search Algorithm
• Optimal size of the block to be skipped is √n, thus resulting in the time complexity O(√n)
• Jump search is better than a linear search (O(n)), but worse than a binary search (O(log n)). The advantage
over the latter is that a jump search only needs to jump backwards once, while a binary can jump
backwards up to log n times. This can be important if a jumping backwards takes significantly more time
than jumping forward.
38
Exponential search
39
Exponential search
• The idea is to start with subarray size 1, compare its last element with x, then try size 2,
• Once we find an index i, we know that the element must be present between i/2 and i.
40
Exponential search
int exponentialSearch(int arr[], int n, int x)
{
// If x is present at firt location itself
if (arr[0] == x)
return 0;
• O(log2 i) for average or worst case. Where i is the location where search key is present.
• Exponential Binary Search is particularly useful for unbounded searches, where size of
array is infinite. Please refer Unbounded Binary Search for an example.
• It works better than Binary Search for bounded arrays, and also when the element to be
searched is closer to the first element.
42