08/03/2016

C Program For Linear Search

Problem:- Linear search in c or C Program to Implement Linear Search or C Program to Search an Array Element using LINEAR SEARCH or C Program to Search an Element in an Array Using Linear Search or Linear Search Program in C or C programming Interview questions and answers or Linear Search in C or C Program Linear Search or C Program/Code for Linear Search into Array or C program for Linear Search or Program to search an element in an array using linear search or Simple Linear Search Example Program in C or Simple Linear Search Example Program Using Functions in C or Linear search in a sorted array.

Logic:- Linear search is a simple search ie. line by line searching. For better understanding a linear search we are taking an example of an array and try to find out an element of an array. We compare element to each and every element of an array if the element matches with array elements then we print matches that element found or we can also print the index of an array. Now we Know that how Linear search worked, Now we are taking an example and performing a Linear Search on it.

Check This:- C++ Program For Linear Search

Explanation:- Let's take an example of an array suppose an array element is 10, 20, 30, 90, 80, 70, 40, 50, 60, 100. We can clearly see that the array size is 10 and an array index is started 0 to 9 and we have to find the element 50 now take a step by step action of our program and see how Linear search works.

Step 1:- First we compare the element 50 to the array first element then second, third and so on. We can clearly see that our elements 50 not matching with the array element 10, so increase an index of an array and compare with the second element then third until our matching element Not Matches with the array elements.

Step 2:- After the element matches with the array element than we print the Message or index that we have found the element, or if our element not matched with the array elements then we can print the message element not found.

Key Notes:- Here are some feature of a Linear search.

1. Linear search worked on Sorted and Unsorted Data.
2. Linear search Worst-case performance =====>O(n)
3. Linear search Best-case performance ======>O(1)
4. Linear Search Average performance =======>O(n)
5. Linear Search Worst-case space complexity==>O(1)

Check:- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.


Solution:-

#include<stdio.h>

main()
{
//C program for linear search

int *a,i,itm,s,flag=0;

printf("==================================");
printf("\nC++ Program For Linear Search\n");
printf("==================================\n");

printf("\nEnter The Size Of An Array :");

scanf("%d",&s);

a=(int*)malloc(s*sizeof(int));

printf("\nEnter An Elements Of An Array\n");

for(i=0;i<s;i++)
{
scanf("%d",&a[i]);
}

printf("\nEnter The Number You Want To Search In An Array :");
scanf("%d",&itm);

for(i=0;i<s;i++)
{
if(a[i]==itm)
{
flag=1;
}
}

if(flag==1)
{
printf("\nNumber Found In an Array \n");
}
else 
{
printf("\nNumber Not Found In an Array \n");
}
}

Output:-


C Program For Linear Search



You May Like This:-

1. Hacker Rank Solution for 30 Days of Code

2. Hacker Rank solution for Attribute Parser

3. Java Program For Find The Gross Salary Of An Employee

4. C++ Program For School Management System ( SMS Project ) With Source Code

5. Hacker Rank Solution For Mini-Max Sum

6. Hacker Rank Solution For Birthday Cake Candles

7. C++ Program For Store Employee Information And Display Using Structure

8. C Program For Find A Grade Of Given Marks Using Switch Case

9. C Program For HEAP Sort In Ascending Order Also Show Complexity

10. C++ Program For Selection Sort Using Function



Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: