Searching Algorithm
Searching algorithms are methods or procedures used to find a specific item or element
within a collection of data. These algorithms are widely used in computer science and are
crucial for tasks like searching for a particular record in a database, finding an element in
a sorted list, or locating a file on a computer.
These are some commonly used searching algorithms:
1. Linear Search: In this simple algorithm, each element in the collection is
sequentially checked until the desired item is found, or the entire list is traversed. It
is suitable for small-sized or unsorted lists, but its time complexity is O(n) in the
worst case.
2. Binary Search: This algorithm is applicable only to sorted lists. It repeatedly
compares the middle element of the list with the target element and narrows down
the search range by half based on the comparison result. Binary search has a time
complexity of O(log n), making it highly efficient for large sorted lists.
Linear Search Algorithm
The algorithm for linear search is relatively simple. The procedure starts at the very first
index of the input array to be searched.
Step-1: Start from the 0th index of the input array, compare the key value with the value
present in the 0th index.
Step 2: If the value matches with the key, return the position at which the value was
found.
Step-3: If the value does not match with the key, compare the next element in the array.
Step-4: Repeat Step 3 until there is a match found. Return the position at which the
match was found.
Step-5: If it is an unsuccessful search, print that the element is not present in the array
and exit the program.
Pseudocode
procedure linear_search (list, value)
for each item in the list
if match item == value
return the item's location
end if
end for
end procedure
Time Complexity of Linear Search Algorithm:
Best Case: In the best case, the key might be present at the first index. So the best
case complexity is O(1)
Worst Case: In the worst case, the key might be present at the last index i.e., opposite
of the end from which the search has started in the list. So the worst-case complexity
is O(N) where N is the size of the list.
Average Case: O(N)
C Program
#include <stdio.h>
int main() {
int arr = {2, 3, 4, 10, 40};
int i, x = 10;
int pos = -1;
for(i = 0; i < 5; i++){
if(arr[i] == x){
pos = i; break;
}
}
if(res != -1)
printf(“Found at %d”, i+1);
else
printf("Present at Index");
}
Binary Search Algorithm
Binary Search algorithm is an interval searching method that performs the searching in
intervals only. The input taken by the binary search algorithm must always be in a sorted
array since it divides the array into subarrays based on the greater or lower values. The
algorithm follows the procedure below −
Step-1: Select the middle item in the array and compare it with the key value to be
searched. If it is matched, return the position of the median.
Step-2: If it does not match the key value, check if the key value is either greater than or
less than the median value.
Step-3: If the key is greater, perform the search in the right sub-array; but if the key is
lower than the median value, perform the search in the left sub-array.
Step-4: Repeat Steps 1, 2 and 3 iteratively, until the size of sub-array becomes 1.
Step-5: If the key value does not exist in the array, then the algorithm returns an
unsuccessful search.
Pseudocode
Procedure binary_search
A ← sorted array
n ← size of array
x ← value to be searched
Set lowerBound = 1
Set upperBound = n
while x not found
if upperBound < lowerBound
EXIT: x does not exists.
set midPoint = lowerBound + ( upperBound - lowerBound ) / 2
if A[midPoint] < x
set lowerBound = midPoint + 1
if A[midPoint] > x
set upperBound = midPoint - 1
if A[midPoint] = x
EXIT: x found at location midPoint
end while
end procedure
C Program
// C Program to implement Binary Search
#include <stdio.h>
int main(void) {
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10, f = 0;
int n = sizeof(arr) / sizeof(arr[0]);
int low, high, mid;
low = 0;
high = n-1;
while (low <= high) {
mid = low + (high - low) / 2;
if(arr[mid] == x){ // Check if x is present at mid
f = 1; break;
}
if(arr[mid] < x) // If x greater, ignore left half
low = mid + 1;
else // If x is smaller, ignore right half
high = mid - 1;
}
if(f == 1)
printf("Element is present in array");
else
printf("Element is not present in array");
}
Binary Search complexity
Now, let's see the time complexity of Binary search in the best case, average case, and
worst case. We will also see the space complexity of Binary search.
1. Time Complexity
Case Time Complexity
Best Case O(1)
Average Case O(logn)
Worst Case O(logn)
Best Case Complexity - In Binary search, best case occurs when the element to
search is found in first comparison, i.e., when the first middle element itself is the
element to be searched. The best-case time complexity of Binary search is O(1).
Average Case Complexity - The average case time complexity of Binary search
is O(logn).
Worst Case Complexity - In Binary search, the worst case occurs, when we have
to keep reducing the search space till it has only one element. The worst-case time
complexity of Binary search is O(logn).