You are on page 1of 2

S.No: 1 Exp.

Name: Write the code to implement Linear Search Date: 2022-09-10

ID: 210310571886
    Page No:
           
Aim:
Aim: Write a program to implement Linear Search

Theory: In computer science, a linear search or sequential search is a method for finding an element within a
list. It sequentially checks each element of the list until a match is found or the whole list has been searched.

Algorithm:

Linearsearch( Value. List)

If the list is empty, return Nil

else

if the first item of the list has the desired value, return its location ;

else

return linearsearch (value, remainder of the list)

Note : Assume that the array size is 5


Source Code:

linearSearch.c

#include<stdio.h>

int linearsearch(int* , int );

int main()

printf("Enter the elements for array: ");

int arr[6];

for(int i=0; i<5; i++)

scanf("%d",&arr[i]);

int key;

printf("Enter the key: ");

scanf("%d",&key);

int loc;

loc = linearsearch(arr ,key);

if(loc!=-1)

printf("Search is successful\nloc = %d\n",loc);

else

printf("Search is unsuccessful\n");

return 0;

    ITS Engineering College      2021-2025-CSE_3_B1

int linearsearch(int arr[] , int data)

if(arr[0] == '/0')

return -1;

else

for(int i=0; i<5; i++)

if(arr[i]==data)

return i+1;

ID: 210310571886
    Page No:
           
}

return -1;

Execution Results - All test cases have succeeded!

Test Case - 1

User Output
Enter the elements for array: 8 5 4 1 3
Enter the key: 5
Search is successful
loc = 2

Test Case - 2

User Output
Enter the elements for array: 16 25 36 78 54
Enter the key: 26
Search is unsuccessful

    ITS Engineering College      2021-2025-CSE_3_B1

You might also like