1. LINEAR SEARCHING
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
DEPARTMENT: COMPUTER SCIENCE & ENGINEERING(CYBER SECURITY)
NAME OF THE STUDENT: T. TAJESWAR RAO
UNIVERSITY ROLL NO: 27631722026
PAPER NAME: DATA STRUCTURE & ALGORITHMS
PAPER CODE: PCC-CS301
FACULTY NAME:
2. INTRODUCTION
A linear search is the simplest approach employed to search for an
element in a data set. Linear Search is defined as a sequential search
algorithm that starts at one end and goes through each element of
a list until the desired element is found, otherwise the search
continues till the end of the data set. The search is finished and
terminated once the target element is located. If it finds no match,
the algorithm must terminate its execution and return an
appropriate result.
3. HOW LINEAR SEARCH
WORKS
In Linear Search Algorithm,
Every element is considered as a potential match for the
key and checked for the same.
If any element is found equal to the key, the search is
successful and the index of that element is returned.
If no element is found equal to the key, the search
yields “No match found”.
For example: Consider the array arr[] = {10, 50, 30, 70, 80,
20, 90, 40} and key = 30
4. Step 1: Start from the first element (index 0) and compare key with each
element (arr[i]).
• Comparing key with first element arr[0]. SInce not equal, the iterator
moves to the next element as a potential match.
• Comparing key with next element arr[1]. SInce not equal, the iterator
moves to the next element as a potential match.
5. Step 2: Now when comparing arr[2] with key, the value matches. So the
Linear Search Algorithm will yield a successful message and return the
index of the element when key is found (here 2).
6. ALGORITHM
Define a function named search that takes an integer array arr[], an integer N, and an
integer x as arguments.
Inside the search function, use a for loop to iterate over the array from index 0 to N-1.
Inside the for loop, check if the current element of the array is equal to the value of x.
If the current element is equal to x, return the index of the current element.
If the loop finishes without finding the element, return -1.
In the main function, define an integer array arr[] and initialize it with some values.
Define an integer x and set its value to the element we want to search for.
Define an integer N and set its value to the size of the array arr[].
Call the search function with arr, N, and x as arguments and store the returned value in
an integer variable named result.
Use a ternary operator to check if result is equal to -1.
If result is equal to -1, print "Element is not present in array".
If result is not equal to -1, print "Element is present at index result".
7. Source Code:
// C code to linearly search x in arr[].
#include <stdio.h>
int search(int arr[], int N, int x)
{
for (int i = 0; i < N; i++)
if (arr[i] == x)
return i;
return -1;
}
// Driver code
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
int result = search(arr, N, x);
(result == -1)
? printf("Element is not present in array")
: printf("Element is present at index %d", result);
return 0;
}
8. COMPLEXITY OF LINEAR
SEARCH
Time Complexity:
Best Case: In the best case, the key might be
present at the first index. So the best case
complexity is O(1)
Worst Case: In the worst case, the key might be
present at the last index i.e., opposite to the end
from which the search has started in the list. So
the worst-case complexity is O(N) where N is the
size of the list.
9. ADVANTAGES OF LINEAR
SEARCH
• Linear search can be used irrespective of whether the
array is sorted or not. It can be used on arrays of any
data type.
• Does not require any additional memory.
• It is a well-suited algorithm for small datasets.
10. DRAWBACKS OF LINEAR SEARCH
Linear search has a time complexity of O(N), which in
turn makes it slow for large datasets.
Not suitable for large arrays.
11. CONCLUSION
In conclusion, implementing linear search in a project can be a simple and
effective way to search for an element within an array. While it may not be the
most efficient search algorithm for large datasets, it can still be useful for
smaller arrays or when the data is unsorted. Understanding the basics of linear
search can also provide a foundation for learning more complex search
algorithms. Overall, incorporating linear search into a project can improve its
functionality and provide a valuable learning experience.