0% found this document useful (0 votes)
19 views7 pages

Linear Search

Uploaded by

rosemol0099
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)
19 views7 pages

Linear Search

Uploaded by

rosemol0099
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/ 7

LINEAR SEARCH

INTRODUCTION:

A Linear Search, which is also popularly known as a sequential search, is


a process for finding an item in a list of items. This sort of searching
algorithm checks each element of the list one by one until a match is
found or the entire list is searched.

Linear Search in C Programming


In C, we perform a Linear Search to see if a number is present in an array.
It is also known as sequential search in which we compare each element
with the one we're looking for until we find it or when the list runs out.

Algorithm of Linear Search (Approach)


Searching is the method of finding a certain item in a list of items. If the
element is found in the list, the process is considered successful, and the
location of that element is returned; otherwise, the search is considered
unsuccessful.

In Linear Search, the index or search location in the specified array is


found. It starts the search by comparing the search key to the array/first
list's element. If the first element does not match the search key, the next
element will be compared, and so on until the match is discovered or the
array ends. If a match is discovered, the index is returned; otherwise, it
reaches the end of the array or list, indicating that the search key is not
available.

Page 1
For a better understanding of the Linear search, we are going to see an
example.

Example:
Given array = {50, 90, 30, 70, 60};

Assume the search key is 30. Next, scan the array and compare each
element to the search key. The array's first element is 50, but 50 is not
equal to 30, therefore continue on to the next element. The next element
is 90, but it isn't equal to 30, so it's time to move on to the next one. The
array's next element is 30, which is the same as the search key 30, thus
returning the index of the array's current element.

The above example was the case where the search key was present in the
array. Now consider a case where the search key is not present. Let’s
assume that the search key is equal to 10. Compare each element in the
array with the search element. It fails to match with 50, 90, 30, 70, 60,
and eventually reaches the array's conclusion. As a result, return -1 or
print element is not present in the array, indicating that the search key is
not available

Page 2
Code for Linear Search in C program is given below:

#include <stdio.h>

void main()

int num;

int i, key, element_found = 0;

printf("Enter number of elements you would like to take as input: ");

scanf("%d", &num);

int arr[num];

printf("\nEnter all the elements of your choice:");

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

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

printf("\nEnter the key element that you would like to be searched: ");

scanf("%d", &key);

Page 3
/* Linear search starts */

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

if (key == arr[i] )

element_found = 1;

break;

if (element_found == 1)

printf("we got the element at index %d",i+1);

else

printf("we haven’t got element at any index in the array\n");

Output:

Page 4
Explanation of Code:

 In Linear Search, we look for an element or value in an array by


traversing it from the beginning to the end until the required element or
value is discovered.

 The array is searched progressively, and if the key element to be


searched is found in the array, the position is returned; otherwise, -1 is
returned.

 We haven't created a function particularly for Linear Search in this


C program; instead, we can check for the presence of an element in an
array in the main function.

 We traverse the array starting at the 0th index and increasing in


order of index, breaking the loop there and printing the element's
position in the array, but if the element requested is not present in the
array, we simply display "Element is not present in the array."

 If we had written a separate Linear Search function and the element


could not be found in the array, we would have returned -1 or print

Page 5
“Element is not present in array” indicating that the element was not
present.

Characteristics of Linear Search:

 Time Complexity: O(n), where n is the number of elements in the


array. This is because in the worst case, every element must be
checked.
 Space Complexity: O(1), as it requires no additional storage
beyond the input array and a few variables.
 Best Use Case: Suitable for small datasets or unsorted arrays
where the overhead of more complex search algorithms is
unnecessary

Page 6
Conclusion
In C, Linear Search involves traversing a list or array sequentially to see
if an entry is there. The goal is to begin traversing the array and compare
items of the array one by one, starting with the first element, until a match
is discovered or the array's end is reached.

Page 7

You might also like