Linear Search

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Linear Search

Students: Jemien Cobre BSIS2A

Subject: Data Structure and Algorithms

Overview

The Linear Search algorithm in C sequentially checks each element of the list until
the key element is found or the entire list has been traversed. Therefore, it is
known as a sequential search.

Introduction to Linear Search in C

Linear Search is the most basic method of searching an element from a list. It is
also known as sequential search, as it sequentially checks each element of the list
until the key element is found or the entire list has been traversed. It uses
conditional statements and relational operators to find whether the given element
is present in the list or not. It is easy to learn and implement.

Approach to Implement Linear Search Algorithm in C

Take input of the element that is going to be searched. It can be referred to as a


key element.

Compare every element of the list with the key element starting from the leftmost
end of the list.

If any element of the list matches with the key, return the index of that element.

If the entire list has been traversed and none of the elements matched with the
key, then return -1, which specifies the key element is not present in the list.

Implementation of Linear Search Program in C

#include<stdio.h>

int main() {

// declaration of the array and other variables

int arr[20], size, key, i, index;


printf("Number of elements in the list: ");

scanf("%d", &size);

printf("Enter elements of the list: ");

// loop for the input of elements from 0 to number of elements-1

for (i = 0; i < size; i++)

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

printf("Enter the element to search ie. key element: ");

scanf("%d", &key);

// loop for traversing the array from 0 to the number of elements-1

for (index = 0; index < size; index++)

if (arr[index] == key) // comparing each element with the key element

break; // cursor out of the loop when a key element found

if (index < size) // condition to check whether previous loop partially traversed or
not

printf("Key element found at index %d", index); // printing the index if key found

else

printf("Key element not found");

return 0;

Output:

Number of elements in the list: 5


Enter elements of the list: 1 2 3 4 5

Enter the element to search ie. key element: 4

A key element found at index 3

Explanation:

✔️Declaration of an array and other variables.

✔️Take the input of the size of the array.

✔️Take the input of the array with help of a loop.

✔️Take the input of the element to search for i.e. key element.

✔️The array will be traversed with the help of a loop from 0 to size-1.

✳️Inside the loop, Apply a condition that whether the current element is equal to
the key element or not

✳️If the current element matches with the key element, then get out of the loop
using a break statement, and if is not matched, the condition keeps checking the
elements until the end of the array.

✔️After the cursor gets out of the loop, check whether the previous loop traversed
the array partially or not with the help of a condition that the index of the previous
loop is less than the size of the array or not.

✔️If the condition returns true, then print the index of the key element.

✔️If the condition returns false, then print the key element not found.

You might also like