public class LinearSearch { public static void printArray(int[] a) { System.out.print("\nIndex i: "); for(int i=0; i< a.length; i++) System.out.printf("%2d, ", i); System.out.print("\nElement a[i]: "); for(int i=0; i< a.length; i++) System.out.printf("%2d, ", a[i]); System.out.println("\n\n"); } public static void main(String[] args) { int[] a= { 4, 21, 36, 14, 62, 91, 8, 22, 7, 81, 77, 10}; printArray(a); // Now do the Linear Search int target = 62; //int target = 72; // Try this target next int idx=-1; System.out.println("\nSearching for target=" + target + " ..."); for(int i=0; i< a.length; i++) { if(a[i] == target) { idx=i; break; } } if(idx == -1) System.out.println("Target not found."); else System.out.println("Target found at index: " + idx); } }