Join the growing community of curious minds on IDNLearn.com. Ask any question and receive accurate, in-depth responses from our dedicated team of experts.

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 :

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.