Linear Search Algorithm (With Code) : Updated On Dec 13, 2023 12:26 IST
Linear Search Algorithm (With Code) : Updated On Dec 13, 2023 12:26 IST
Shiksha Online
Updated on Dec 13, 2023 12:26 IST
Have you ever wondered how to find a specific item in a list without sorting it? The linear search
algorithm does just that, sequentially checking each element of the list until it finds the target
item or reaches the end of the list. This simple yet effective method is widely used when data is
unsorted or constantly changing. Let us understand more!
The linear search algorithm is a simple and straightforward method used to find a
particular element in a list. It works by sequentially checking each element of the list
until the desired element is found or the end of the list is reached.
Table of Content
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Dec-20 23.
C
C++
Python
Java
Improved Approach
C
C++
Python
Java
Here, we are going to implement Linear Search in C, C++, Python, and Java using two
approaches.
Simple Approach
Improved Approach
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Dec-20 23.
T ime Complexity: O(n)
C Program
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Dec-20 23.
Copy code
// Main function
int main() {
int array[] = {2, 4, 0, 8, 6, 10, 23, 1, 9};
int x = 23;
int n = sizeof (array) / sizeof (array[0]); // Calculate the number of elements in
the array
if (result == -1) {
print f ("Element not f ound");
} else {
print f ("Element f ound at index: %d", result );
}
ret urn 0;
}
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Dec-20 23.
Output
C++ Program
Copy code
// Main function
int main() {
int array[] = {2, 4, 7, 8, 0, 1, 9};
int x = 8;
int n = sizeof (array) / sizeof (array[0]); // Calculate the number of elements in
the array
if (result == -1) {
cout << "Element not f ound";
} else {
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Dec-20 23.
} else {
cout << "Element f ound at index: " << result ;
}
ret urn 0;
}
Output
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Dec-20 23.
Copy code
Output
Java Program
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Dec-20 23.
Copy code
class LinearSearch {
public st at ic int linearSearch(int [] array, int x) {
int n = array.lengt h;
f or (int i = 0; i < n; i++) {
if (array[i] == x) {
ret urn i; // Return the index where the element is found
}
}
ret urn -1; // Element not found
}
if (result == -1) {
Syst em.out .print ("Element not f ound");
} else {
Syst em.out .print ("Element f ound at index: " + result );
}
}
}
Output
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Dec-20 23.
Improve Linear Search Worst-Case Complexity – where the search_element is at the
end of the array.
C Program
Copy code
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Dec-20 23.
int right = lengt h - 1;
int main() {
int arr[] = {2, 4, 7, 8, 0, 1, 9};
int element = 1;
int lengt h = sizeof (arr) / sizeof (arr[0]); // Correct way to get the number of
elements in an array
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Dec-20 23.
search(arr, lengt h, element );
}
Output
C++ Program
Copy code
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Dec-20 23.
wit h " << lengt h - right << " At t empt " << endl;
break;
}
lef t ++;
right --;
}
int main() {
int arr[] = {2, 4, 7, 8, 0, 1, 9};
int element = 0;
int lengt h = sizeof (arr) / sizeof (arr[0]); // Correctly calculate the length
Output
Copy code
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Dec-20 23.
# Run loop from 0 to right
while lef t <= right :
# If element is found with left variable
if arr[lef t ] == Element :
posit ion = lef t
print ("Element f ound in Array at ", posit ion + 1, "Posit ion wit h", lef t + 1,
"At t empt ")
break
lef t += 1
right -= 1
# Driver code
arr = [1, 2, 3, 4, 5, 7, 8]
element = 5
# Function call
search(arr, element )
Output
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Dec-20 23.
Java Program
Copy code
import java.io.*;
class Improved_LS {
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Dec-20 23.
break;
}
lef t ++;
right --;
}
Output
Linear search can be used on single and multidimensional arrays, whereas binary
search can only be implemented on the one-dimensional array. Linear search is less
efficient when we consider large data sets. Binary search is more efficient than linear
search in the case of large data sets.
However, actually, both types of searches have their own merits and demerits. The
answer to this question depends on the dataset entirely. If we’re searching for an
element present at the extreme ends of the list, then a Linear search will run better
than a binary search.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Dec-20 23.
Conclusion
Hope the above article helped you understand linear search better. If you have any
queries feel free to reach us at the link below. For more such articles, stay tuned to
Shiksha Online.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 14-Dec-20 23.