Linear Search (or Sequential Search) is a straightforward method for searching an element within a collection of elements:
- Algorithm: The steps involved in Linear Search are:
- Start: Begin at the first element of the collection.
- Compare: Compare the current element with the desired element.
- Found: If a match is found, return the index of the current element.
- Move: If no match, move to the next element in the collection.
- Repeat: Repeat steps 2-4 until the end of the collection.
- Not found: If the end of the collection is reached without finding the desired element, return that the element is not present.
- Example: Consider an array
arr[] = {10, 50, 30, 70, 80, 20, 90, 40} and we want to find the element 30. Linear Search checks each element sequentially until it finds the element or reaches the end.
Implementation in C++:
#include <bits/stdc++.h>
using namespace std;
int search(int arr[], int N, int x) {
for (int i = 0; i < N; i++) {
if (arr[i] == x)
return i;
}
return -1;
}
int main(void) {
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
int result = search(arr, N, x);
(result == -1)
? cout << "Element is not present in array"
: cout << "Element is present at index " << result;
return 0;
}
Output: Element is present at index 3
Time and Space Complexity:
- Time Complexity:
- Best Case: O(1) - When the element is found at the first position.
- Worst Case: O(N) - When the element is found at the last position or is not present.
- Average Case: O(N)
- Auxiliary Space: O(1) - Constant space used beyond the input array.
Linear Search is simple but effective for small datasets or unordered lists where other search algorithms like binary search are not applicable.
Binary Search is an efficient searching algorithm used to locate a target value within a sorted array:
- Algorithm: Binary search operates by repeatedly dividing the search interval in half. The steps are:
- Divide: Find the middle index
mid of the array.
- Compare: Compare the middle element with the target value.
- Search space adjustment: Based on the comparison, adjust the search space to the left or right half.
- Repeat: Continue the process until the target value is found or the search space is exhausted.
- Found: If the target is found at
mid, return the index.
- Not found: If the target is not found, return -1.
- Example: Consider an array
arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91} and the target 23. Binary search efficiently locates 23 by halving the search space iteratively.
Iterative Binary Search Implementation in C++:
#include <bits/stdc++.h>
using namespace std;
// Iterative Binary Search function
int binarySearch(int arr[], int low, int high, int x) {
while (low <= high) {
int mid = low + (high - low) / 2;
// Check if x is present at mid
if (arr[mid] == x)
return mid;
// If x greater, ignore left half
if (arr[mid] < x)
low = mid + 1;
// If x is smaller, ignore right half
else
high = mid - 1;
}
// If we reach here, then element was not present
return -1;
}
// Driver code
int main(void) {
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, 0, n - 1, x);
(result == -1)
? cout << "Element is not present in array"
: cout << "Element is present at index " << result;
return 0;
}
Output: Element is present at index 3
Recursive Binary Search Implementation in C++:
#include <bits/stdc++.h>
using namespace std;
// Recursive Binary Search function
int binarySearch(int arr[], int low, int high, int x) {
if (high >= low) {
int mid = low + (high - low) / 2;
// If the element is present at the middle
if (arr[mid] == x)
return mid;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, low, mid - 1, x);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid + 1, high, x);
}
// If element is not present in array
return -1;
}
// Driver code
int main(void) {
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, 0, n - 1, x);
(result == -1)
? cout << "Element is not present in array"
: cout << "Element is present at index " << result;
return 0;
}
Output: Element is present at index 3
Complexity Analysis:
- Time Complexity:
- Best Case: O(1) - When the element is found at the middle position.
- Average Case: O(log N) - Efficiently halves the search space.
- Worst Case: O(log N) - When the element is not present or located at the far ends.
- Auxiliary Space: O(1) - Constant space is used beyond the input array.
Binary Search is highly efficient for searching in large sorted datasets and is a fundamental algorithm in computer science.