Exhaustive Search Strategy

Exhaustive search, also known as brute-force search, is a straightforward algorithmic approach that checks every possible solution to a problem in order to find the best one. In C language, an exhaustive search can be implemented using a nested loop structure.

Here’s an example of an exhaustive search algorithm in C language:

#include <stdio.h>

int main() {
    int arr[] = {1, 3, 5, 7, 9}; // array to search in
    int n = sizeof(arr) / sizeof(arr[0]); // size of array
    int target = 7; // target element to search for
    int i, j;

    for (i = 0; i < n; i++) {
        for (j = i + 1; j < n; j++) {
            if (arr[i] + arr[j] == target) {
                printf("Found %d and %d at index %d and %d\n", arr[i], arr[j], i, j);
                break; // exit inner loop
            }
        }
    }

    return 0;
}

This program searches for a pair of elements in an array whose sum equals a given target value. The nested loop structure checks every possible pair of elements until a matching pair is found. If a matching pair is found, the program prints the indices and values of the pair.

The outer loop iterates over each element of the array, and the inner loop iterates over the remaining elements to the right of the current element. This ensures that each pair of elements is checked exactly once.

Exhaustive search can be computationally expensive for large problems, but it’s a simple and reliable approach that can be used for small-scale problems or as a baseline for more sophisticated algorithms.

Add a Comment

Your email address will not be published. Required fields are marked *