Page 94 - CTS - CSA TP - Volume 2
P. 94

COMPUTER SOFTWARE APPLICATION - CITS





           Explanation:
           1  Binary Search Method:

              •  The binarySearch method performs a binary search on a sorted array to find the index of the target
                 element.
              •  It takes two parameters: the array (arr) and the target element (target).

              •  The method uses a while loop with left and right pointers to narrow down the search space until the target
                 element is found or the search space is empty.

              •  If the target element is found, the method returns the index; otherwise, it returns -1.
           2  Main Method:

              •  The main method is the entry point of the program.
              •  It uses a Scanner to take user input for the size of the array, array elements, and the target element to be
                 searched.
           3  Array Input and Bubble Sort:

              •  The program prompts the user to enter the size of the array and then input the array elements.

              •  After taking the array elements, a simple bubble sort algorithm is applied to sort the array in ascending
                 order.

           4  Sorted Array Display:
              •  The sorted array is displayed in ascending order after the bubble sort.

           5  Target Element Input:
              •  The user is prompted to enter the target element that needs to be searched in the array.

           6  Binary Search Execution:
              •  The binarySearch method is called with the sorted array and the target element as parameters to perform
                 the binary search.
           7  Search Result Display:

              •  The result of the binary search is displayed:

              •  If the target element is found, the program prints its index.
              •  If the target element is not found, a corresponding message is printed.

           8  Scanner Closing:
              •  The Scanner is closed to release system resources.




















                                                           79
                                CITS : IT & ITES - Computer Software Application - Exercise 93
   89   90   91   92   93   94   95   96   97   98   99