diff --git a/searching/linear_search.c b/searching/linear_search.c index 6982512cc4..320be5fab3 100644 --- a/searching/linear_search.c +++ b/searching/linear_search.c @@ -1,11 +1,34 @@ +/* Linear Search Algorithm + * + * This program demonstrates the linear search technique, where each element + * of the array is checked sequentially until the target value is found. + * + * Time Complexity: + * - Best case: O(1) + * - Worst case: O(n) + * + * Space Complexity: + * - O(1) + * + * Works on both sorted and unsorted arrays. + */ #include #include - +/* + * Performs linear search on an integer array. + * + * @param arr Pointer to the array + * @param size Number of elements in the array + * @param val Value to search for + * + * @return 1 if the value is found, otherwise 0 + */ int linearsearch(int *arr, int size, int val) { int i; for (i = 0; i < size; i++) { + // Compare each element with the target value if (arr[i] == val) return 1; } @@ -22,7 +45,7 @@ int main() 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// + // loop terminates printf("Enter the value to be searched:\n"); scanf("%d", &v); // Taking input the value to be searched