Page 383 - computer science (868) class 11
P. 383

51              // number is greater than the number in middle
                  52          }

                  53
                  54          public static void main()

                  55          {
                  56              Scanner sc1=new Scanner(System.in);

                  57              int s,v;
                  58              System.out.println("Enter the number of elements in the array:");
                  59              s = sc1.nextInt();

                  60              BinSearch obj = new BinSearch(s);

                  61              obj.fillarray();
                  62              System.out.println("Enter the element you want to search:");
                  63              v = sc1.nextInt();

                  64              obj.sort();
                  65              int p=obj.bin_search(0,s,v);

                  66              if(p==-1)
                  67                  System.out.println(v+"not found in array ");

                  68              else
                  69                  System.out.println(v+" found in location: " +(p+1));

                  70          }
                  71      }


                 The output of the preceding program is as follows:
                 Enter the number of elements in the array:
                 7

                 Enter 7 elements
                 12 67 23 99 45 34 89
                 Enter the element you want to search:

                 45
                 Sorted array
                 12 23 34 45 67 89 99
                 45 found in location: 4













                                                                                                                       381
                                                                                                           Recursion   381
   378   379   380   381   382   383   384   385   386   387   388