Linear Search
Linear search is the simplest search algorithm. It checks each element in the array sequentially until the target is found or the end is reached. Time complexity: O(n) in worst case. Works on unsorted arrays.
Java implementation:
public static int linearSearch(int[] arr, int target) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) return i;
}
return -1;
}
Linear search is useful for small datasets or when the array is unsorted. It's also the basis for many other algorithms.
Two Minute Drill
- Linear search checks each element one by one.
- Time O(n), space O(1).
- Works on any array (sorted or unsorted).
- Simple but inefficient for large data.
Need more clarification?
Drop us an email at career@quipoinfotech.com
