Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
programming in C
1. Data structures in C..
ADITHYA. M
2ND SEM, BCA
19NCBC1042
National college
Linear
search
method
2. Linear search..
Searching is the process of looking for something.Linear search is the simplest search algorithm and
often called sequential search.In computer science,a linear search or sequential search is a method for
finding an element within a list.We search an element or value in a given array or list by traversing the
array from the starting,till the desired element or value is found.Every item is checked and if a match is
found then that particular item is returned,otherwise the search continues till the end of the data collection
otherwise the algorithm returns NULL.
3. Simple steps for linear search..
• Step 1 - Read the search element from the user.
• Step 2 - Compare the search element with the first element in the list.
• Step 3 - If both are matched,then display "Given element is found!!!" and terminate the function
• Step 4 - If both are not matched,then compare search element with the next element in the list.
• Step 5 - Repeat steps 3 and 4 until search element is compared with last element in the list.
• Step 6 - If last element in the list also doesn't match, then display "Element is not found!!!" and
terminate the function.
4. Implementing Linear Search..
Traverse the array using a for loop.
In every iteration,compare the target value with the current value of the array.
If the values match,return the current index of the array.
If the values do not match,move on to the next array element.
If no match is found,return -1
5.
6. Algorithm..
• Step 1:Select the first element as the current element.
• Step 2:Compare the current element with the target element.If matches,then go
to step 5.
• Step 3:If there is a next element,then set current element to next element and
go to Step 2.
• Step 4:Target element not found.Go to Step 6.
• Step 5:Target element found and return location.
• Step 6:Exit process
7. Applications..
• Linear search is usually very simple to implement,and is practical when the list has only a few
elements,or when performing a single search in an un-ordered list.
• When many values have to be searched in the same list,it often pays to pre-process the list in order to
use a faster method.For example,one may sort the list and use binary search,or build an efficient
search data structure from it.
• As a result,even though in theory other search algorithms may be faster than linear search (for
instance binary search),in practice even on medium-sized arrays (around 100 items or less) it might
be infeasible to use anything else.On larger arrays,it only makes sense to use other,faster search
methods if the data is large enough,because the initial time to prepare (sort) the data is comparable
to many linear searches.
8. Comparison between Binary Search and Linear Search:…
• Binary Search requires the input data to be sorted;Linear Search doesn’t
• Binary Search requires an ordering comparison;Linear Search only requires equality
comparisons
• Binary Search has complexity O(log n);Linear search has complexity O(n) as discussed earlier
• Binary Search requires random access to the data;Linear Search only requires sequential access
(this can be very important — it means a Linear Search can stream data of arbitrary size)