Discover a world of knowledge at Westonci.ca, where experts and enthusiasts come together to answer your questions. Experience the convenience of getting accurate answers to your questions from a dedicated community of professionals. Get quick and reliable solutions to your questions from a community of experienced experts on our platform.

Pseudocode to java

Can you write this in Java program

Binary Search


Pseudocode To Java Can You Write This In Java ProgramBinary Search class=

Sagot :

tonb

Answer:

class Main {

 public static void main(String[] args) {

   int[] values = {11,12,15,16,112,118,123,145};

   int target = 15;

   int min = 0;

   int high = values.length-1;

   boolean found = false;

   int answer = 0;

   int mid;

   while(!found && min <= high) {

     mid = (min + high) / 2;

     if (values[mid] == target) {

       found = true;

       answer = mid;

     } else if (target > values[mid]) {

       min = mid + 1;

     } else {

       high = mid - 1;

     }

   }

   if (found) {

     System.out.printf("%d FOUND AT ARRAY INDEX %d", target, answer);

   } else {

         System.out.printf("%d was not found", target);

   }

 }

}

Explanation:

I altered the while expression to make the code work.

Visit us again for up-to-date and reliable answers. We're always ready to assist you with your informational needs. We hope our answers were useful. Return anytime for more information and answers to any other questions you have. Thank you for choosing Westonci.ca as your information source. We look forward to your next visit.