Q:

C Program For Linear Search

belongs to collection: Searching C Programs

0
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.


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.
 
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)

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#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

==================================

Enter The Size Of An Array :6

Enter An Elements Of An Array

18

24

46

64

77

81

Enter The Number You Want To Search In An Array :77

Number Found In an Array 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C Program For Binary Search... >>