Performing Binary Search on Java char Array Example

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

Visit Java Example Forums to request Java examples or ask Java questions!
OR
Try out our new Java Search Engine




Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!