You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given an array arr[] of n elements, write a function to search a given element x in arr[].
4
+
5
+
A simple approach is to do linear search, i.e
6
+
7
+
1.Start from the leftmost element of arr[] and one by one compare x with each element of arr[]
8
+
9
+
2.If x matches with an element, return the index.
10
+
11
+
3.If x doesn’t match with any of elements, return -1.
12
+
13
+
14
+
# The time complexity of above algorithm is O(n).
15
+
16
+
Linear search is rarely used practically because other search algorithms such as the binary search algorithm and hash tables allow significantly faster searching comparison to Linear search.
0 commit comments