Linear Java Search Example









 This   is     simple   search example .  Where   we  use   a given  array search .  To  find  out
  correct  position    the  search  element in the array.



public class LinearSearch {

    public static int linerSearch(int[] arr, int key){
        
        int size = arr.length;
        for(int i=0;i
            if(arr[i] == key){
                return i;
              }
         }
        return -1;
       }
    
     public static void main(String a[]){
        
        int[] arr1= {25,45,34,55,234,10,32,90};
        int searchKey = 55;
        System.out.println("Key "+searchKey+" found at index: "+linerSearch(arr1, searchKey));
        int[] arr2= {134,446,421,595,2134,41,367,190};
        searchKey = 190;
        System.out.println("Key "+searchKey+" found at index: "+linerSearch(arr2, searchKey));
    }
}

Program Output :.

Key 55 found at index: 3
Key 190 found at index: 7
Linear Java Search Example Linear  Java Search  Example Reviewed by Mukesh Jha on 11:21 PM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.