Skip to Content

Performing Binary Search on Java double Array Example

  • strict warning: Non-static method view::load() should not be called statically in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/views.module on line 906.
  • strict warning: Declaration of views_handler_argument::init() should be compatible with views_handler::init(&$view, $options) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_argument.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter_boolean_operator.inc on line 0.
  • strict warning: Declaration of views_plugin_style_default::options() should be compatible with views_object::options() in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_style_default.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.

  1. /*
  2.   Performing Binary Search on Java double Array Example
  3.   This java example shows how to perform binary search for an element of
  4.   java double array using Arrays class.
  5. */
  6.  
  7. import java.util.Arrays;
  8.  
  9. public class BinarySearchDoubleArrayExample {
  10.  
  11.   public static void main(String[] args) {
  12.     //create double array
  13.     double doubleArray[] = {1.23,2.10,4.74,5.34};
  14.    
  15.     /*
  16.       To perform binary search on double array use
  17.       int binarySearch(double[] b, double value) of Arrays class.
  18.       This method searches the double array for specified double value
  19.       using binary search algorithm.
  20.      
  21.       Please note that the double array MUST BE SORTED before it can be searched
  22.       using binarySearch method.
  23.      
  24.       This method returns the index of the value to be searched, if found in the
  25.       array.
  26.       Otherwise it returns (- (X) - 1)
  27.       where X is the index where the the search value would be inserted.
  28.       i.e. index of first element that is grater than the search value
  29.       or array.length, if all elements of an array are less than the
  30.       search value.
  31.     */
  32.    
  33.     //sort double array using Arrays.sort method
  34.     Arrays.sort(doubleArray);
  35.    
  36.     //value to search
  37.     double searchValue = 4.74;
  38.    
  39.     //since 4.74 is present in double array, index of it would be returned
  40.     int intResult = Arrays.binarySearch(doubleArray,searchValue);
  41.     System.out.println("Result of binary search of 4.74 is : " + intResult);
  42.    
  43.     //lets search something which is not in double array !
  44.     searchValue = 3.33;
  45.     intResult = Arrays.binarySearch(doubleArray,searchValue);
  46.     System.out.println("Result of binary search of 3.33 is : " + intResult);
  47.  
  48.   }
  49. }
  50.  
  51. /*
  52. Output would be
  53. Result of binary search of 4.74 is : 2
  54. Result of binary search of 3.33 is : -3
  55. */