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