Linear Search Visualizer
Watch how Linear Search sequentially checks each element until the target is found or array ends
Array Size: 8Comparisons: 0
64
[0]25
[1]12
[2]22
[3]11
[4]90
[5]45
[6]33
[7]Not Checked
Currently Checking
Not a Match
Already Searched
Found!
Linear Search Implementation:
public static int linearSearch(int[] arr, int target) {
// Check each element sequentially
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
return i; // Found! Return index
}
}
return -1; // Not found
}
// Time Complexity: O(n) - Linear time
// Space Complexity: O(1) - Constant space
// Best Case: O(1) - Element at first position
// Worst Case: O(n) - Element at last position or not found
// Average Case: O(n/2) - Element in middle
// Works on: Sorted AND Unsorted arrays💡 Linear Search Characteristics:
- Simple and easy to implement
- Works on both sorted and unsorted arrays
- No preprocessing required
- Checks every element sequentially from start to end
- Inefficient for large datasets (O(n) time)
- Best for small arrays or unsorted data
🆚 Linear vs Binary Search:
- Linear: O(n) time, works on unsorted arrays
- Binary: O(log n) time, requires sorted array
- Use Linear for: Small or unsorted datasets
- Use Binary for: Large sorted datasets