Linear search (Sequential search) algorithm:
A method to find specific element within a list or an array. It checks sequentially each
element of the list one by one, until target element is found or the end of the list
reached.
Characteristics:
1. Simplicity : Simple to understand and use
2. No sorting is required
3. Time complexity is O(n) Best case O(1), Worst case O(n)
4. Suitability : Suitable for small list or unsorted list
Algo Steps:
Assumption: An array of capacity max_sisze with data
Step 1: Start
Step 2: Read key value to be searched
Step 3: Compare the key value for 0th Element of an array
Step 4: Repeat the step 3 until key value found or reached to the end of an array
Step 5: Display the result.
Step 6: Stop
An Example :
Consider an array with data as:
34 10 66 27 47 8 55 78
Key value to be searched: 47
Step-1:
34 10 66 27 47 8 55 78
Start from 0th index. Compare the key value with the value at 0th index.
i.e. 47 is not equal to 34. Move to the next element.
Step-2:
34 10 66 27 47 8 55 78
Compare the key value with the value at 1st index.
i.e. 47 is not equal to 10. Move to the next element.
Step-3:
34 10 66 27 47 8 55 78
Compare the key value with the value at 2nd index.
i.e. 47 is not equal to 66. Move to the next element.
Step-4:
34 10 66 27 47 8 55 78
Compare the key value with the value at 3rd index.
i.e. 47 is not equal to 27. Move to the next element.
Step-5:
34 10 66 27 47 8 55 78
Compare the key value with the value at 4th index.
i.e. 47 is equal to 47.
Key value element found at 5th position in an array.
Stop the process.
Binary search (Binary search) algorithm:
Binary search algorithm is fast search algorithm with time complexity O(log(n)). It
works on principle, Divide and Conquer. The requirement for the algorithm is array
data should be sorted. Binary search looks for a particular key value by comparing the
middle most item of the collection. If the middle item has value greater than the key
value, the right side sub-array is searched for key value otherwise left sub-array is
searched. The process is recursively continued until the key value found of size of an
sub-array reduces to zero.
Algo:
Assumption:
1. Data array arr[] is of capacity maxsize and data are in sorted in ascending order.
2. Key value is the element to be searched.
3. lb, up and mid are be the integer values to denote respective positions.
Algo-Steps
Step-1: start
Step-2: Read key value
Step-3: Set lb = 0; ub=maxsize –1
Step-4: if lb > ub then goto step 9
Step-5: Set mid = (lb + ub)/2; integer division
Step-6: if key = arr[mid]
print “Element found at position” + (mid+1)
goto Step-9
Step-7: if key >= arr[mid]
Set lb = mid + 1
else
Set ub = mid – 1
Step-8: goto step-4
Step-9: Stop
Applications of Binary Search:
1. Searching in sorted data
2. Finding Lower and upper bound.
3. Root finding algorithm
4. Game Development.
5. Database Indexing
6. Network Routing
7. Resource Allocation
An Example of Binary search
1. Example: Searching for a number in a sorted array
2. Consider the following sorted array of numbers:
[2, 5, 8, 12, 16, 23, 38, 56, 72, 91]
Let the target value to be found be 23.
3. The steps involved in the binary search algorithm are as follows:
4. Initialize Pointers: Set a low pointer to the first element's index (0) and
a high pointer to the last element's index (9).
5. Find the Middle Element: Calculate the middle index: mid = (low + high) / 2.
o Iteration 1: mid = (0 + 9) / 2 = 4 (integer division).
o The element at mid (index 4) is 16.
6. Compare and Adjust Search Space: Compare the middle element with the
target value.
o Is 16 equal to 23? No.
o Is 16 less than 23? Yes.
o Since the list is sorted and the middle element is less than the target, the
target must be in the right half of the array. Discard the left half by
updating low to mid + 1 (which is 5).
o The new search space is [23, 38, 56, 72, 91].
7. Repeat the Process: Continue the loop with the new low and high pointers (low
= 5, high = 9).
o Iteration 2: mid = (5 + 9) / 2 = 7.
o The element at mid (index 7) is 56.
8. Compare and Adjust Again:
o Is 56 equal to 23? No.
o Is 56 less than 23? No, it's greater.
o The target must be in the left half of the current search space. Discard the
right half by updating high to mid - 1 (which is 6).
o The new search space is [23, 38].
9. Repeat Until Found: Continue with the new low and high pointers (low = 5, high
= 6).
o Iteration 3: mid = (5 + 6) / 2 = 5.
o The element at mid (index 5) is 23.
o A match is found! The algorithm returns the index 5.
If the element is not found by the time the low pointer exceeds the high pointer,
the search is unsuccessful. This method is significantly faster than checking
every element one by one (linear search), especially for large datasets, with a
time complexity of O(log n).
The interpolation search
The interpolation search algorithm is an improved version of binary search used to
find a target value in a sorted array. It is particularly efficient when the elements in the
array are uniformly distributed (evenly spaced), outperforming binary search in such
cases.
Unlike binary search, which always checks the middle element, interpolation search
estimates the probable position of the target element based on its value relative to
the values at the endpoints of the current search range. This is similar to how a
person might search a physical dictionary, knowing that words starting with "A" are at
the beginning and words with "Z" are at the end.
The core of the algorithm is the formula used to calculate the next probable position
(pos):
This formula determines the next position to check. The algorithm repeatedly
calculates ‘pos’ and adjusts the search range until the target is found.
Time Complexity
The performance of interpolation search varies based on the data. It offers an average
time complexity of O(log log n) for uniformly distributed data, but can reach O(n) in
the worst case with non-uniform data.
Algorithm: Assumptions
Arr → Array list
Maxsize → Size of Arr
Key → Target Value
Algo steps:
Step 1: Start
Step-2: Set lb → 0
Step-3: Set Pos → -1
Step-4: Set ub → Maxsize -1
Step-5: While Key != Arr[Pos]
If lb == ub OR Arr[lb] == Arr[ub] then EXIT: Failure, Target not found
Set Pos = lb + ((Key - Arr[lb]) / (Arr[ub] - Arr[lb])) * (ub - lb)
If Arr[Pos] = Key then print “Success, Target found at Mid” Exit
else if Arr[Pos] <= Key then Set lb to Pos+1
else ub to Pos-1
Step-5: Stop
Example
Consider an array of sorted elements given below −
Solution:
lb = 0, Arr[lb] = 10
ub = 9, Arr[ub] = 44
Key = 19
Since, mid is an index value, we only considering the integer part of the
decimal? That is, mid = 2.
Comparing the key element given, that is 19, to the element present in the mid
index, it is found that both the elements match.
Therefore, the element is found at index 2.