Binary search method an array using Comparable in Java

Problem:

Write a methoe that binary search an array using Comparable in Java.

Output:

Not applicable.

Solution:

public static int binarysearch(Comparable[] items, Comparable target, int first, int last)
  {
    if(first>last)
      return -1;
    else
    {
      int middle=(first+last)/2;
      int value=target.compareTo(items[middle]);
      if(value==0)
        return middle;
      else if(value>0)
        return binarysearch( items,  target,  middle+1,  last);
      else
        return binarysearch( items,  target,  first ,  middle-1);

    }


1 comment :

  1. tumescent liposuction Korea's #1 Liposculpture Clinic. Lydian plastic surgery is the home of VIP patients. Celebrities, Influencers and Diplomats all know and trust Doctor An and Lydian plastic surgery clinic to provide detailed results.

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact