JOIN
Get Time
forums   
Search | Watch Thread  |  My Post History  |  My Watches  |  User Settings
View: Flat (newest first)  | Threaded  | Tree
Previous Thread  |  Next Thread
binary search non-recursively in Java. | Reply
It uses a static function BusquedaAlgoritmo class.

Remember that to work properly the values ​​of the array must be sorted.


BusquedaAlgoritmo class {
     public static int search (int [] array, int data) {
  int start = 0;
  int end = arreglo.length - 1;
  int pos;
  while (start <= end) {
      pos = (start + end) / 2;
      if (array [pos] == data)
        return pos;
      else if (array [pos] <data) {
   start = pos +1;
      Else {}
   end = pos-1;
      }
  }
  return -1;
     }
}

BusquedaBinaria {public class
     public static void main (String args []) {
  / / Fill accordance
  int [] ages = new int [35];
  for (int i = 0; i ><edades.length, i + +)
      ages [i] = i * i;

  / / Display array.
  for (int i = 0; i ><edades.length, i + +)
      System.out.println ("ages [" + i + "]:" + ages [i]);

  int result = BusquedaAlgoritmo.buscar (ages, 9);

  if (result! = -1) {
      System.out.println ("Found:" + result);
  Else {}
      System.out.println ("The data is not in the array, or the array is ordered.");
  }
 
     }
}
I hope you serve something>
Re: binary search non-recursively in Java. (response to post by Jhoan Marquez C) | Reply
very good companion publications desire more codified arrangements exercises
Re: binary search non-recursively in Java. (response to post by Jhoan Marquez C) | Reply
nice publication.
RSS