Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Binary Search Help!

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Binary Search Help!

    So I need to do this:

    A Java program to find the value 45.3 from this list ={-3,10,5,24,45.3,10.5} using the binary search method.

    And I have absolutely nothing. I need help as soon as possible. Thank you.


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,102
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Binary Search Help!

    I recommend giving it a try. We are not a code service so no one is going to do this for you, but we will try and help along the way if you post code and explain your problem or question thoroughly.

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Binary Search Help!

    Do I need to write an array to start it, to end it, or not at all? ~ Sorry, I kept getting errors with the code I had.

  4. #4
    Junior Member
    Join Date
    Mar 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Binary Search Help!

    public class BinarySearch
    {
      public static final NOT_FOUND = -1;
      public static int binarySearch (Integer [] a, int x)
       {
          int low=0;
          int high = a.length - 1;
          int mid;
          while (low <= high)
          {
    	 mid = (low + high) / 2;
    	 if (a[mid] .comareTo (x)<0)
    	    low = mid + 1;
    	 else if (a[mid] .comareTo (x) > 0)
    	    high = mid - 1;
    	 else
    	    return mid;
          {
    	return NOT_FOUND;
       }
     
       public static void main(String[] args)
       {
          int SIZE = 8;
          Integer[] a = new Integer[ SIZE ];
          for (int i=0; i<SIZE; i++)
          a[i] = new Integer(i * 2)
          for (int i=0; i<SIZE*2, i++)
    	 System.out.println("Found" + i + " at " +)
    binarySearch(a, new Integer (i)));
       }
    }

    Heres the Binary Search code that I have, do I write a separate code to sort the numbers? I tried, but the decimals causes it to come back with an error:
    public class BinarySearchArray {
        public static void main(String[] args) {
            int unsortedArray[] = {10, -3,5, 24, 45.3, 10.5};
            int i;
     
            bubbleSort(unsortedArray, unsortedArray.length); 
     
            System.out.println("After sorting, the list elements are: ");
     
            for(i=0; i<unsortedArray.length; i++) {
                System.out.print(unsortedArray[i] + " ");
            }
        }
     
        private static void bubbleSort(int[] unsortedArray, int length) {
            int temp, counter, index;
     
            for(counter=0; counter<length-1; counter++) { //Loop once for each element in the array.
                for(index=0; index<length-1-counter; index++) { //Once for each element, minus the counter.
                    if(unsortedArray[index] > unsortedArray[index+1]) { //Test if need a swap or not.
                        temp = unsortedArray[index]; //These three lines just swap the two elements:
                        unsortedArray[index] = unsortedArray[index+1];
                        unsortedArray[index+1] = temp;
                    }
                }
            }
        }
    }
    Last edited by helloworld922; March 14th, 2011 at 10:08 PM.

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Binary Search Help!

    A binary search requires some sort of sorted structure. Often this is done by creating a binary search tree, but you can implement a binary search on an array by manipulating the indices.

    For an ascending sorted array implementation (note: the sorted part is extremely important, you could implement this algorithm for a descending sorted array with a few changes):
    1. Set the left index to 0, set the right index to the last element of the array
    2. while left index != right index:
    3. Compute the middle index by averaging the left and right.
    4. If the number at the middle index is greater than the desired number, set the right index to the middle index
    5. else if the number at the middle index is smaller than the desired number, set the left index to the middle index
    6. else you've luckily found the number. break out of the while loop (note: this is optional and an unnecessary step)
    7. end while loop

    All of the indices (left, right, and middle) all hold the index of the desired element (if it exists), just check to see if the number at that index is the number you want.

    edit:

    The problem you have is because you didn't define the type of NOT_FOUND. simply define it as an int.

    The second problem is that you're trying to put non-integer data types into an integer array. Integers must not have any numbers after the decimal. The simple solution is to replace every int[] with double[], which is a floating-point data type array.
    Last edited by helloworld922; March 14th, 2011 at 10:10 PM.

  6. #6
    Junior Member
    Join Date
    Mar 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Binary Search Help!

    What does a. length mean?

  7. #7
    Junior Member
    Join Date
    Mar 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Binary Search Help!

    public class BinarySearch
    {
      public static final int = 45.3;
      public static int binarySearch (Integer [] a, int x)
       {
          int low=0;
          int high = a.length; 45.3;
          int mid;
          while (low <= high)
          {
    	 mid = (low + high) / 2;
    	 if (a[mid] .comareTo (x)<0)
    	    low = mid + 1;
    	 else if (a[mid] .comareTo (x) > 0)
    	    high = mid - 1;
    	 else
    	    return mid;
          {
    	return int;
       }
     
       public static void main(String[] args);
       {
          int SIZE = 45.3;
          Integer[] a = new Integer[ SIZE ];
          for (int i=0; i<SIZE; i++);
          a[i] = new Integer(i * 2);
          for (int i=0; i<SIZE*2, i++);
    	 System.out.println("Found" + i + " at " +);
    binarySearch(a, new Integer (i)));
       }
    }

    So I'm still getting errors, is there STILL something wrong with my code?
    Last edited by helloworld922; March 15th, 2011 at 01:17 PM.

  8. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Binary Search Help!

    Please properly format your code with highlight tags. Could you post the error messages you're getting?

    You're not suppose to return int, you just need to define that NOT_FOUND is an integer type. Java is strong-typed so you must explicitly define the type of all variables.

    public static final int NOT_FOUND;
    //... later on when you want to use NOT_FOUND
    return NOT_FOUND;

  9. #9
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Binary Search Help!

    Quote Originally Posted by Allicat View Post
    public class BinarySearch
    {
      public static final int = 45.3;
      public static int binarySearch (Integer [] a, int x)
       {
          int low=0;
          int high = a.length; 45.3;
          int mid;
          while (low <= high)
          {
    	 mid = (low + high) / 2;
    	 if (a[mid] .comareTo (x)<0)
    	    low = mid + 1;
    	 else if (a[mid] .comareTo (x) > 0)
    	    high = mid - 1;
    	 else
    	    return mid;
          {
    	return int;
       }
     
       public static void main(String[] args);
       {
          int SIZE = 45.3;
          Integer[] a = new Integer[ SIZE ];
          for (int i=0; i<SIZE; i++);
          a[i] = new Integer(i * 2);
          for (int i=0; i<SIZE*2, i++);
    	 System.out.println("Found" + i + " at " +);
    binarySearch(a, new Integer (i)));
       }
    }

    So I'm still getting errors, is there STILL something wrong with my code?
    You forgot to give one of the variables a name .
    public static final int = 45.3;
    Also, "int" values cannot have decimals. The decimals will just be ignored if you do, if I recall correctly. Use a "double" instead. Like this:
    public static final double varname = 45.3;

    There is also a random 45.3 that I am confused about.
    int high = a.length; 45.3;
    The 45.3 shouldn´t be there, since it does nothing and may cause errors depending on how picky the compiler is (have not tested it, lol).

    And about these two lines:
    int SIZE = 45.3;
    Integer[] a = new Integer[ SIZE ];
    As said before, an "int" cannot store decimal values. And other then that, an array cannot have a size of 45.3. They can only have non-decimal and non-negative sizes.

    I hope this helps a bit .
    Last edited by Kerr; March 15th, 2011 at 04:14 PM.

Similar Threads

  1. Binary Search Tree in Java [HELP]
    By alan in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 5th, 2011, 05:44 AM
  2. Binary Search Tree
    By lex25288 in forum Algorithms & Recursion
    Replies: 3
    Last Post: January 19th, 2011, 08:10 AM
  3. Binary Search Loop
    By Tarakara in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 29th, 2010, 08:22 AM
  4. Binary Search Tree
    By Koren3 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 12th, 2009, 08:27 AM
  5. Binary Search Tree implementation
    By Ceasar in forum Java Theory & Questions
    Replies: 3
    Last Post: October 9th, 2009, 12:23 AM