Slidesgo Unlocking The Secrets of Linear Search A Journey Through C Language 20250105093740AuSW
Slidesgo Unlocking The Secrets of Linear Search A Journey Through C Language 20250105093740AuSW
SEaRch By
CH.SRINIV
AS
24911A6611
Introduction to Linear
Search
is a fundamental
algorithm in computer science.
This presentation will take you on
a
journey through the t
explore how this simple o
yet
effective search method
works. Get ready to unlock the
secrets
behind its mechanics
and applications!
What is Linear
Search?
Linear Search is an
that checks each element in
a list until the
desired element is found. It's
simple and intuitive, making it
a great starting point for
beginners in programming.
Let's dive into how it
operates step by step.
Source Code of Linear Search
#include <stdio.h>
int linearSearch(int a[ ], int n, int val) {
// Going through array sequencially
for (int i = 0; i < n; i++)
{
if (a[i] == val)
return i+1;
}
return -1;
}
int main() {
int a[ ] = {70, 40, 30, 11, 57, 41, 25, 14, 52}; // given array
int val = 41; // value to be searched
int n = sizeof(a) / sizeof(a[0]); // size of array
int res = linearSearch(a, n, val); // Store result
printf("The elements of the array are - ");
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\nElement to be searched is - %d", val);
if (res == -1)
printf("\nElement is not present in the array");
else
printf("\
nElement is present at %d position of array", res);
return 0;
How Linear Search
Works
In Linear Search, we start at
the of the array
and check each element
one by one. If we find the
target, we return its
; if not, we
continue until we reach the
end. This method is
straightforward but can be
inefficient for large
datasets.
Implementing in C Language
Let's see how to implement
Linear Search in C. The basic
structure involves a loop that
iterates through the array. We'll
use simple conditional
statements to check for the
target value. This is an excellent
opportunity to practice your
coding skills!
Pros and Cons of Linear Search