Searching...
Tuesday, 28 February 2017

Linear Search example

19:56

package
com.vinod.test; public class LinerSearch { public static void main(String[] args) { int[] arr = { 1, 3, 5, 7, 9, 12, 16, 18 }; System.out.println("Position of 5=" + getPositionUsingLinearSearch(arr, 5)); } public static int getPositionUsingLinearSearch(int[] arr, int key){ int size = arr.length; for(int i=0;i<size;i++){ if(arr[i] == key){ return i; } } return -1; } }

0 comments: