Java program for recursive binary sort


SUBMITTED BY: Guest

DATE: Dec. 25, 2013, 8:04 a.m.

FORMAT: Java

SIZE: 979 Bytes

HITS: 1580

  1. import java.util.Scanner;
  2. class Binary
  3. {
  4. int A[];
  5. int n,l,u;
  6. public Binary(int nn)
  7. {
  8. n=nn;
  9. A=new int[n];
  10. l=0;
  11. u=n-1;
  12. }
  13. public void readdata()
  14. {
  15. Scanner in=new Scanner(System.in);
  16. System.out.println("Enter the elments of the array in ascending order: ");
  17. for(int i=0;i<n;i++)
  18. {
  19. A[i]=in.nextInt();
  20. }
  21. }
  22. public int binarysearch(int v)
  23. {
  24. if(l>u)
  25. return -1;
  26. else
  27. {
  28. int mid=(l+u)/2;
  29. if(A[mid]==v)
  30. return mid+1;
  31. else if(v<A[mid])
  32. {
  33. u=mid-1;
  34. return binarysearch(v);
  35. }
  36. else
  37. {
  38. l=mid+1;
  39. return binarysearch(v);
  40. }
  41. }
  42. }
  43. }

comments powered by Disqus