Guest User

Untitled

a guest
May 19th, 2013
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.79 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.Arrays;
  3.  
  4. class BinarySearch
  5. {
  6.     static int i;
  7.  
  8.     public static void main(String[] args)
  9.     {
  10.         Scanner a = new Scanner (System.in);
  11.         System.out.println("Enter a number to find its position in the array.");
  12.         int n = a.nextInt();
  13.         searchBinary(n);
  14.     }
  15.  
  16.     public static void searchBinary(int searchValue)
  17.     {
  18.         int a[] = {123,321,456,235,721,7420,69271,51,356,137,11,13,5,6,1};
  19.  
  20.         Arrays.sort(a);
  21.  
  22.         System.out.println("The sorted Array is: ");
  23.         for(i=0; i<a.length; i++)
  24.         {
  25.             System.out.println(a[i]);
  26.         }
  27.  
  28.         int returnValue = Arrays.binarySearch(a, searchValue);
  29.  
  30.         System.out.println("The index of the element is : " + returnValue);
  31.  
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment