Advertisement
Md_Sakib_Hossain

Linear Search Indexed Array

Mar 8th, 2021
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.39 KB | None | 0 0
  1. public class LinearSearchIA{
  2.     public static void main(String[] args) {
  3.         int data[]={1,2,3,4,5};
  4.         int element=3;
  5.         int loc=0;
  6.         boolean x=false;
  7.  
  8.         while (loc<data.length-1) {
  9.             if(data[loc] == element){
  10.                 x=true;
  11.                 break;
  12.             }
  13.             loc++;
  14.         }
  15.         if(x){
  16.             System.out.print("Location of array is "+(loc+1));
  17.         }else{
  18.             System.out.print("Location of array is "+x);
  19.         }
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement