Page 91 - CITS - CSA - TP (Volume 2) - Hindi
P. 91

कं  ूटर सॉ वेयर ए ीके शन - CITS



           6  mid = (start+end)/2
           7  if array[ mid ] == target then return mid
           8  if array[ mid ] < target then start = mid+1

           9  if array[ mid ] > target then end = mid-1
           10  if start<=end then goto step 6
           11  return -1 as Not element found
           12  Exit
           यहाँ एक Java  ो ाम है जो उपयोगकता  को कीबोड  के  मा म से ऐरे एलीम ट को इनपुट करने और बाइनरी सच  करने की अनुमित देता है:
           import java.util.Scanner;

           public class BinarySearch {
               public static int binarySearch(int[] arr, int target) {
                   int left = 0;
                   int right = arr.length - 1;



                   while (left <= right) {
                       int mid = left + (right - left) / 2;


                       if (arr[mid] == target)
                           return mid;
                       else if (arr[mid] < target)
                           left = mid + 1;

                       else
                           right = mid - 1;
                   }


                   return -1;

               }


               public static void main(String[] args) {
                   Scanner scanner = new Scanner(System.in);


                   // Prompt the user to enter the size of the array

                   System.out.print(“Enter the size of the array: “);
                   int size = scanner.nextInt();


                   // Create an array of the specified size
                   int[] arr = new int[size];



                   // Prompt the user to enter array elements
                   System.out.println(“Enter the elements of the array :”);
                   for (int i = 0; i < size; i++) {

                                                           77

                                   CITS : IT & ITES - कं  ूटर सॉ वेयर ए ीके शन - अ ास 93
   86   87   88   89   90   91   92   93   94   95   96