The linear search algorithm works in the following straightforward manner:
- Starting from the collection's commencement.
- Compared with the target element, compare the current element.
- Return the element's index or location if a match is discovered.
- Return the appropriate indication (e.g., -1) if the collection's conclusion is reached without a match.
We require an array or list of items to iterate through for performing linear search in C programming. Let's delve into an illustration to comprehend the functioning of linear search:
Example:
#include <stdio.h>
int linearSearch(int arr[], int n, int target) {
int i;
for (i = 0; i< n; i++) {
if (arr[i] == target) {
return i; // Element found at index i
}
}
return -1; // Element not found
}
int main() {
int arr[] = {10, 2, 8, 5, 17};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 8;
int result = linearSearch(arr, n, target);
if (result == -1) {
printf("Element not found in the array.\n");
} else {
printf("Element found at index: %d\n", result);
}
return 0;
}
Assuming the designated element is 8, we will now utilize the sample array [10, 2, 8, 5, 17]. Executing the code produces the ensuing outcome:
Element found at index: 2
The number '8' was identified in this case using the linear search method and was located at index 2 within the array.
Let's dissect the implementation process in detail:
- To use the printf function , we include the h library .
- The array arr , the array's size n , and the element to be searched are the three arguments provided to the linearSearch function .
- We create the integer variable i and use a for loop to iterate through the array.
- We compare each element of the array with the target element inside the loop.
- If a match is discovered, we return the element's location's index.
- We return -1 to denote that the element was not found if, after searching the full array, no matches were discovered.
- We declare an array called arr with a few random members in the main function.
- By dividing the array's overall size by the size of a single member, we may get the array's n-dimensional size.
- The target element we wish to look for is specified (in this example, 8).
- The array, size, and target are passed as arguments when we use the linearSearch function , and we store the result in the result variable.
- Finally, we evaluate the value of the outcome. We print a message explaining that the element was not found if it is -1. If not, we display the index at which the element was located.
Conclusion:
In conclusion, linear search stands as a foundational method for finding elements within a dataset, providing a basic structure for implementing search functionalities in programming. While it may not be the optimal choice for large or ordered datasets, linear search serves as an essential starting point for novices delving into the realm of search algorithms.
We have the option to iteratively traverse an array or list in the C programming language, examining each item against the specified target element, in search of a match until either a match is discovered or all elements have been checked. This approach is straightforward and accessible, making it suitable for individuals new to programming.
While linear search remains ideal for smaller or disorganized lists, it may not be the most efficient choice for larger datasets. In cases of sorted or structured data, more sophisticated search techniques such as binary search and hash-based methods offer quicker search times. Nevertheless, understanding the concept of linear search is crucial as it forms the foundation for more complex algorithms.