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

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





                       System.out.print(“Element “ + (i + 1) + “: “);
                      arr[i] = scanner.nextInt();

                   }
                   // Bubble sort in ascending order
                   for (int i = 0; i < size - 1; i++) {

                       for (int j = 0; j < size - i - 1; j++) {
                           if (arr[j] > arr[j + 1]) {
                               // Swap arr[j] and arr[j+1]

                               int temp = arr[j];
                               arr[j] = arr[j + 1];
                               arr[j + 1] = temp;

                           }
                       }

                   }
                    // Display the sorted array in ascending order
                   System.out.print(“Array elements in ascending order:”);
                   for (int i = 0; i < size; i++) {

                       System.out.print(arr[i]+” “);
                   }

                   // Prompt the user to enter the target element to be searched
                   System.out.println();
                   System.out.print(“Enter the target element to search: “);

                   int target = scanner.nextInt();


                   // Perform binary search

                   int index = binarySearch(arr, target);


                   // Print the result of the search

                   if (index != -1)
                       System.out.println(“Element “ + target + “ found at index “ + (index=index+1));

                   else
                       System.out.println(“Element “ + target + “ not found in the array.”);



                   scanner.close(); // Close the scanner
               }
           }




                                                           78

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