It is the simplest searching algorithm.
A sequential search is made over all items one by one.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class Serach{ public static void main(String[] args){ int[] arr = {3, 2, 5, 4, 7, 9, 1}; int target = 9; System.out.println(linearSearch(arr,target)); } public static String linearSearch(int[] arr, int target){ for(int i=0; i<arr.length-1; i++){ if(arr[i]==target) return "found at index "+i; } return "not found"; } } |