Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BinarySearchIA{
- public static void main(String[] args) {
- int DATA[]={1,2,3,4,5,6,7,8,9,10};
- int BEG=0; // start index of array
- int END=9; // the length of array
- int ITEM=3; // your item
- int MID=0; // must initailize you variable else you'll get 100% error
- while(BEG<=END && DATA[MID]!= ITEM){//It's run over condition
- if(ITEM<DATA[MID]) // if your element is small than the
- {//operation MID than do -1 else do +1
- END = MID -1;
- }else{
- BEG = MID +1;
- }
- MID=((BEG+END)/2);//Here is your operation
- }
- if(DATA[MID] == ITEM){ //You find do what you want to do
- System.out.print(DATA[MID]);
- }else{
- System.out.print("Not founded");//else your wish
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement