Linear Search Algorithm

The Linear Search Algorithm, also known as Sequential Search, is a simple and widely used technique for searching an element in a list or an array. It works by iterating through each element of the list or array, comparing the desired value with the current element. When a match is found, the algorithm returns the index of the matching element or indicates that the value has been found. If the desired value is not found after iterating through the entire list or array, the algorithm returns an indication that the value is not present. This method is particularly useful for small data sets or unsorted data, as it does not require any prior organization or sorting of the data. One of the key features of the Linear Search Algorithm is its simplicity, making it easy to implement and understand. However, its performance can be relatively slow for large data sets, as it may require examining every element before determining that the desired value is not present. The time complexity of the Linear Search Algorithm is O(n), where n is the number of elements in the list or array. In the best-case scenario, the desired value is found at the first position, requiring only one comparison, while in the worst-case scenario, the desired value is not present, requiring n comparisons. Despite its limitations, the Linear Search Algorithm remains a popular choice for basic searching tasks and serves as a foundation for understanding more complex search algorithms.
#include <stdio.h>

int linearsearch(int *arr, int size, int val){
	int i;
	for (i = 0; i < size; i++){
		if (arr[i] == val)
			return 1;
	}
	return 0;
}

void main(){
	int n,i,v;
	printf("Enter the size of the array:\n");
	scanf("%d",&n); //Taking input for the size of Array

	int a[n];
	printf("Enter the contents for an array of size %d:\n", n);
	for (i = 0; i < n; i++)	scanf("%d", &a[i]);// accepts the values of array elements until the loop terminates//

	printf("Enter the value to be searched:\n");
	scanf("%d", &v); //Taking input the value to be searched
	if (linearsearch(a,n,v))
		printf("Value %d is in the array.\n", v);
	else
		printf("Value %d is not in the array.\n", v);
}

LANGUAGE:

DARK MODE: