0% found this document useful (0 votes)
15 views4 pages

DSA Linear Searching

The document outlines the Linear Search algorithm, detailing its step-by-step process for finding a target element in an array. It includes a C code implementation of the algorithm and explains how the function works, along with a real-world analogy of searching for a book. Linear Search is highlighted as a simple and effective method, particularly for small datasets and unsorted arrays.

Uploaded by

mitaligole02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

DSA Linear Searching

The document outlines the Linear Search algorithm, detailing its step-by-step process for finding a target element in an array. It includes a C code implementation of the algorithm and explains how the function works, along with a real-world analogy of searching for a book. Linear Search is highlighted as a simple and effective method, particularly for small datasets and unsorted arrays.

Uploaded by

mitaligole02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Linear Search Algorithm

Step-by-Step Algorithm
1. Step 1:

• Start from the first element of the array.


2. Step 2:
• Compare the target element with each element of the array one by
one.

3. Step 3:
• If a match is found, return the index of the element.
4. Step 4:

• If the element is not found in the array, return -1.

1
C Code for Linear Search
1 // C program to implement Linear Search
2 # include < stdio .h >
3

4 int linearSearch ( int arr [] , int n , int target ) {


5 for ( int i = 0; i < n ; i ++) {
6 if ( arr [ i ] == target ) {
7 return i ; // Element found
8 }
9 }
10 return -1; // Element not found
11 }
12

13 int main () {
14 int arr [] = {10 , 20 , 30 , 40 , 50};
15 int n = sizeof ( arr ) / sizeof ( arr [0]) ;
16 int target = 30;
17

18 int result = linearSearch ( arr , n , target ) ;


19

20 if ( result != -1) {
21 printf ( " Element found at index : % d \ n " , result ) ;
22 } else {
23 printf ( " Element not found in the array .\ n " ) ;
24 }
25

26 return 0;
27 }

Explanation of Code
1. The linearSearch function takes the array, its size, and the target ele-
ment as inputs.

2. A for loop is used to iterate through the array.


3. Each element is compared with the target value.
4. If a match is found, its index is returned.

5. If no match is found, the function returns -1.

2
Real-World Example: Looking for a Book in a
Stack
Imagine you are looking for a specific book in a stack:

1. You start from the top of the stack.


2. Check each book one by one.
3. If it’s the book you’re looking for, stop.
4. If you reach the bottom and haven’t found it, conclude it’s not there.

This is similar to how Linear Search works by checking each element until a
match is found.

3
Diagrammatic Representation of Linear Search
• Array: [10, 20, 30, 40, 50]
• Target: 30
• Step-by-step:
– Compare with 10 → Not a match
– Compare with 20 → Not a match
– Compare with 30 → Match found
– Return index 2

Why Use Linear Search?


• Simple and easy to implement.
• No need for the array to be sorted.
• Best suited for small datasets.

You might also like