Professional Documents
Culture Documents
Introduction
Linear Search
Binary Search
Jump Search
Interpolation Search
• Searching Algorithm is an algorithm made up of a series of
Step1: Start from the leftmost element of array and one by one
compare the element/value(x) with each element of array.
Search for 9
8 12 5 9 2
Compare
X= 9
8 12 5 9 2
i
Compare
X= 9
8 12 5 9 2
i
Compare
X= 9
8 12 5 9 2
i
Compare
X= 9
8 12 5 9 2
i
Found at index = 3
8 12 5 9 2
Code
4 6 8 9 1
Binary Search
Binary Search is the most popular Search algorithm.
It is efficient and also one of the most commonly
used techniques that is used to solve problems.
Binary search uses sorted array by repeatedly
dividing the search interval in half.
Algorithm:
Step3: Else If x is greater than the mid element, search on right half.
Step4: Else If x is smaller than the mid element. search on left half.
Assume the following Array:
Search for 40
2 3 10 30 40 50 70
Compare
X = 40
2 3 10 30 40 50 70
L mid R
Compare
X = 40
2 3 10 30 40 50 70
L mid R
Compare
X = 40
2 3 10 30 40 50 70
L R
mid
x=40 , found at index = 4
2 3 10 30 40 50 70
C++ Implementation (iterative method):
#include <iostream> int main(void) {
using namespace std; int array[] = {3, 4, 5, 6, 7, 8, 9};
int x = 4;
int binarySearch(int array[], int x, int low, int high) {
int n = sizeof(array) / sizeof(array[0]);
// Repeat until the pointers low and high meet int result = binarySearch(array, x, 0, n - 1);
each other if (result == -1)
while (low <= high) { cout<<"Not found";
int mid = low + (high - low) / 2; else
Cout<<“Element is found at index“<< result;
if (array[mid] == x) }
return mid;
if (array[mid] < x)
low = mid + 1;
Implement the binary search using recursive
else method…
high = mid - 1;
}
return -1;
}
C++ Implementation:
// A recursive binary search function. It returns location of x in given array arr[l..r] is present, otherwise -1
// If element is smaller than mid, then it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
Search for 77
0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
Calculate:
0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
Jump size = 4
0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
Jump size = 4
0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
Jump size = 4
0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
Jump size = 4
0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
Jump size = 4
0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
jump back a step
0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
C++ Code
Time Complexity: O(√n)
Interpolation Search
• The Interpolation Search is an improvement over Binary
Search for instances. On the other hand interpolation
search may go to different locations according the value of
key being searched.
• The data collection should be in a sorted form and equally distributed.
• There are cases where the location of target data may be known in
advance
• Binary Search always goes to the middle element to check. On the other hand,
interpolation search may go to different locations according to the value of the
key being searched.
• For example, if the value of the key is closer to the last element, interpolation
search is likely to start search toward the end side.
Algorithm:
Step1: In a loop, calculate the value of “pos” using the position formula.
Step3: If the item is less than arr[pos], calculate the position of the left
sub-array. Otherwise calculate the same in the right sub-array.
Search for x= 18
10 12 13 16 18 19 20 21 22 23 24 33 35 42 47
Calculate pos = lo + [ (x-arr[lo])*(hi-lo) / (arr[hi]-arr[Lo]) ]
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
10 12 13 16 18 19 20 21 22 23 24 33 35 42 47
Calculate pos = 3
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
10 12 13 16 18 19 20 21 22 23 24 33 35 42 47
Calculate pos = lo + [ (x-arr[lo])*(hi-lo) / (arr[hi]-arr[Lo]) ]
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
10 12 13 16 18 19 20 21 22 23 24 33 35 42 47
Calculate pos = 4
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
10 12 13 16 18 19 20 21 22 23 24 33 35 42 47
C++ Code
• Time Complexity: If elements are uniformly distributed,
• then O (log (log n)). In worst case it can take up to O(n).