Binary search an array using Comparable in Java

Problem:

Write a java program that uses binary search to search array
using Comparable in Java

Output:

Not applicable.

Solution:

public class Linearsearch
{
  public static void main(String[] args)
  {
    String[] stuff={"a","b","c"};
    System.out.println(linearsearch(stuff,"b", 0));
    System.out.println(binarysearch(stuff,"a", 0, stuff.length-1));
  }
  public static int linearsearch(Comparable[] items, Comparable target, int posfirst)
  {
    if(posfirst==items.length)
      return -1;
    else if(items[posfirst].compareTo(target)==0)
    {
      return posfirst;
    }
    else
      return linearsearch(items, target,  (posfirst+1));
  }


No comments :

Post a Comment

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