Here you will get program for linear search in C++.
In linear search algorithm, we compare targeted element with each element of the array. If the element is found then its position is displayed.
Linear search is a method for searching a value within a array. It sequentially checks one by one of the array for the target element until a match is found or until all the elements have been searched of that array. Simple Linear Search Example Using functions Program (Sequential search). The time complexity of the above algorithm is O(n). Please refer complete article on Linear Search for more details! Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Linear search in C to find whether a number is present in an array. If it's present, then at what location it occurs. It is also known as a sequential search. It is straightforward and works as follows: we compare each element with the element to search until we find it or the list ends. Linear search for multiple occurrences and using a function. Example to Implement Linear Search. The program code to implement a linear search is as given below. This program has been written in C programming. Let’s go through the following program so as to understand how it helps us find the requisite element in the list using the linear search algorithm.
The worst case time complexity for linear search is O(n).
Program for Linear Search in C++
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 | { cout<<'How many elements?'; cout<<'nEnter elements of the arrayn'; for(i=0;i<n;++i) cin>>x; for(i=0;i<n;++i) if(a[i]x) flag=1; } cout<<'nElement is found at position '<<i+1; cout<<'nElement not found'; return0; |
Output
How many elements?4
Enter elements of the array
5 9 12 4
5 9 12 4
Enter element to search:9
Element is found at position 2
You May Also Like:
Today we will learn Linear Search Program in C. So before starting, we will make a quick overview of Linear Search. So,
What is a Linear Search?
This is the simplest form of searching. It can be applied to sequential storage structures like files, arrays or linked lists. Linear search is also called as sequential search.
Procedure:
In this method, the searching begins from the first element or record. The required key value is compared with the record key. Searching continues sequentially till the record with a matching key value is found or if the data ends.
Linear Seach Algorithm:
If there are ‘n’ records in a table r(0) to r(n-1) each having a key-value k(0) to k(n-1), the algorithm searches for the required “key” and returns the position ‘i’ of the record r(i) where the “key” is found.
1st Step: START.
2nd Step: i=0.
3rd Step: Read the value of the key to be searched.
4th Step: if k(i)key then display “Record found at position i”. Blur game skachat. and go to step 8.
5th Step: Increment i.
6th Step: If i<n then go to step 4.
7th Step: display “No match found”.
8th Step: STOP.
![C++ C++](/uploads/1/1/8/3/118304542/419136186.png)
Efficiency of Linear Search:
Best Case: Record found at position 1. Hence only 1 comparison is performed. Thus the best case time complexity is O(1).
Worst Case: Record is not found in the table, n comparisons are performed. Hence, the worst-case time complexity is O(n).
Advantages:
It is a very simple method.
It does not require any additional data structure.
Also, it does not require any additional data structure.
Disadvantages:
It is very inefficient since its time complexity is O(n). If n is very large, this method is very slow.
The following program searches for an element among a set of ‘n’ integers.
1. Linear Search Program in C
Output:
Explanation:
In the above linear search program, we search for value given in the array by traversing the array from starting until we find the value.
The element is searched sequentially and the position is returned when the element is found.
If the element is found in the array then the compiler will print “The number found at i position” else compiler will print “Number not found in the array”.
Expected Input and Output:
Average case:
If the searched element is other than the first and the last element. Time complexity is O(n).
Best Case:
If the searched element is at first position. Time Complexity is O(1).
Worst Case:
Howl (2015) Movie Info: Full Name: Howl (2015) Language: Dual Audio (Hindi-English)Size: 1GB Quality: 720p WEB-DL HDFormat: Mkv IMDb Rating: 5.4/10 Genre: Action, Adventure, Horror Director:Paul Hyett Cast: Elliot Cowan, Rosie Day, Calvin A. Dean Howl (2015) Story:A train that departs from London hits a deer on the track while passing through the countryside. Crank 2 full movie in hindi free download 720p.
If the searched element is at the last position. Time Complexity is O(n).
Improving Efficiency of Linear Search:
There are several techniques by which efficiency can be improved.
Organize the data in a sorted manner. If the record is not in the table, the search will be terminated when the record with key>required key is found. Thus, the entire table need not be searched.
Arrange the records by their frequency of access i.e, the records with the highest frequency of access will be stored first. This will reduce the time taken to access a record needed frequently. However, the frequencies have to be known in advance.
Move-to-the-front method: In this case, the records are dynamically reorganized such that if there is a retrieval, that record is moved to the front of the table. Thus the records accessed more frequently occupy positions at the beginning of the table.
Linear Search on Sorted Data:
If the data is sorted on the key values, the efficiency of sequential search improves. The searching will be done till a match is found or a greater key is reached.
Algorithm of Sorted Data:
1st Step: START.
2nd Step: Read “Key” to be searched.
3rd Step: i=0.
4th Step: If k(i)key display “Record foud at position i”.
5th Step: If k(i)>key then go to step 8.
6th Step: Increment i.
7th Step: If i<n then go to step 4.
8th Step: Display “Record not found”.
9th Step: STOP.
The following function illustrates how linear search is performed on sorted data, any sorting method can be used to sort the data.
2. Linear Search C Program for Multiple Occurrences
In this program the compiler will print the positions of the array where the searched element is repeated.
Linear Search C++ Code
Output:
3. Linear Search in C Using function
In this program we will declare a user defined function to search the given element in the array.
Output:
Linear Search Example Program In C++
Also Read: