Searching refers to the process of finding a specific element in a collection of data, such as an array or a list. Searching algorithms are essential in various applications, such as looking up data in a database, finding elements in an array, or navigating through graphs. Below are the most common searching algorithms in C++.
Linear Search is the simplest searching algorithm. It checks each element of the array sequentially from the beginning until the target element is found or the entire array has been searched.
#include <iostream>
using namespace std;
int linearSearch(int arr[], int n, int target) {
for (int i = 0; i < n; i++) {
if (arr[i] == target) {
return i; // Return the index if found
}
}
return -1; // Return -1 if element is not found
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 22;
int result = linearSearch(arr, n, target);
if (result != -1) {
cout << "Element found at index: " << result << endl;
} else {
cout << "Element not found" << endl;
}
return 0;
}
Element found at index: 3
Binary Search is a more efficient algorithm for finding an element in a sorted array. It repeatedly divides the search interval in half and compares the target with the middle element.
low) and one at the end (high) of the array.high pointer.low pointer.#include <iostream>
using namespace std;
int binarySearch(int arr[], int n, int target) {
int low = 0, high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target) {
return mid; // Element found
}
else if (arr[mid] < target) {
low = mid + 1; // Search in the right half
}
else {
high = mid - 1; // Search in the left half
}
}
return -1; // Element not found
}
int main() {
int arr[] = {11, 12, 22, 25, 64};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 22;
int result = binarySearch(arr, n, target);
if (result != -1) {
cout << "Element found at index: " << result << endl;
} else {
cout << "Element not found" << endl;
}
return 0;
}
Element found at index: 2
Jump Search is an algorithm that works on sorted arrays. It works by jumping ahead by a fixed block size and then performing a linear search within the block where the target element might lie. This reduces the number of comparisons compared to linear search.
#include <iostream>
#include <cmath>
using namespace std;
int jumpSearch(int arr[], int n, int target) {
int step = sqrt(n); // Calculate block size
int prev = 0;
// Jump ahead by step until we reach an element greater than the target or end of the array
while (arr[min(step, n) - 1] < target) {
prev = step;
step += sqrt(n);
if (prev >= n) {
return -1;
}
}
// Perform linear search within the block
for (int i = prev; i < min(step, n); i++) {
if (arr[i] == target) {
return i;
}
}
return -1; // Element not found
}
int main() {
int arr[] = {1, 4, 7, 12, 16, 19, 23, 29, 31, 35};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 19;
int result = jumpSearch(arr, n, target);
if (result != -1) {
cout << "Element found at index: " << result << endl;
} else {
cout << "Element not found" << endl;
}
return 0;
}
Element found at index: 5
Exponential Search is an extension of binary search. It is used for searching in sorted arrays, where it first finds the range where the element could exist by doubling the size of the range until the element is less than the current value.
i = 1, and double the index value (i.e., i = 2, 4, 8, 16, ...) until the target is less than the element at the current index.#include <iostream>
#include <cmath>
using namespace std;
int binarySearch(int arr[], int left, int right, int target) {
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target)
return mid;
if (arr[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
int exponentialSearch(int arr[], int n, int target) {
if (arr[0] == target) return 0;
int i = 1;
while (i < n && arr[i] <= target) {
i *= 2;
}
return binarySearch(arr, i / 2, min(i, n - 1), target);
}
int main() {
int arr[] = {1, 4, 7, 12, 16, 19, 23, 29, 31, 35};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 19;
int result = exponentialSearch(arr, n, target);
if (result != -1) {
cout << "Element found at index: " << result << endl;
} else {
cout << "Element not found" << endl;
}
return 0;
}
Element found at index: 5
Interpolation Search is an improvement over binary search for uniformly distributed values. Instead of dividing the range in half, it estimates the position of the target value using a formula based on the values at the low and high indices.
Open this section to load past papers