Sequential Search in C Language
Sequential search, also known as linear search, is a simple searching algorithm that searches an array or a list of elements sequentially from start to end until the desired element is found.
In C language, the sequential search algorithm can be implemented using a loop that iterates through the elements of the array or list, comparing each element with the desired element. If the element is found, the loop is terminated, and the position of the element is returned. If the element is not found, the function returns a special value to indicate that the element is not present in the array or list.
Here is an example implementation of sequential search in C language:
int sequentialSearch(int arr[], int n, int x) {
int i;
for (i = 0; i < n; i++) {
if (arr[i] == x) {
return i;
}
}
return -1;
}
In this implementation, arr
is the array of elements to be searched, n
is the size of the array, and x
is the element to be searched. The sequentialSearch
function iterates through the array using a for
loop and compares each element with x
. If the element is found, the function returns its index in the array. If the element is not found, the function returns -1
to indicate that the element is not present in the array.