Advertisement
Md_Sakib_Hossain

Binary Search Indexed Array

Mar 9th, 2021
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.81 KB | None | 0 0
  1. public class BinarySearchIA{
  2.     public static void main(String[] args) {
  3.         int DATA[]={1,2,3,4,5,6,7,8,9,10};
  4.         int BEG=0; // start index of array
  5.         int END=9; // the length of array
  6.         int ITEM=3; // your item
  7.         int MID=0; // must initailize you variable else you'll get 100% error
  8.         while(BEG<=END && DATA[MID]!= ITEM){//It's run over condition
  9.            if(ITEM<DATA[MID]) // if your element is small than the
  10.            {//operation MID than do -1 else do +1
  11.              END = MID -1;
  12.            }else{
  13.              BEG = MID +1;
  14.            }
  15.  
  16.            MID=((BEG+END)/2);//Here is your operation
  17.         }
  18.  
  19.  
  20.         if(DATA[MID] == ITEM){ //You find do what you want to do
  21.          System.out.print(DATA[MID]);
  22.         }else{
  23.          System.out.print("Not founded");//else your wish
  24.         }
  25.  
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement