fork download
  1. import java.util.*;
  2. import java.io.*;
  3. class Linearsearch
  4. {
  5. public static void main(String[] args)
  6. {
  7. int []a={2,4,5,6,7,9,11,13,15,17,19};
  8. int srch=15;
  9. int li=0;
  10. int hi=a.length-1;
  11. int mi=(li+hi)/2;
  12. while(li<hi)
  13. {
  14. if(a[mi]==srch)
  15. {
  16. System.out.println("element found at" +mi+ "index position");
  17. break;
  18. }
  19. else if(a[mi]<srch)
  20. {
  21. li=mi+1;
  22. }
  23. else
  24. {
  25. hi=mi-1;
  26. }
  27. mi=(li+hi)/2;
  28. }
  29. if(li>hi)
  30. {
  31. System.out.println("element is not found");
  32. }
  33. }
  34. }
Success #stdin #stdout 0.13s 35928KB
stdin
Standard input is empty
stdout
element found at8index position