Results 1 to 5 of 5

Thread: Binary search

  1. #1
    jProtagonist is offline Member
    Join Date
    Jul 2014
    Posts
    7
    Rep Power
    0

    Lightbulb Binary search

    Hey guys, so I'm (failing at) writing a program that searches an array using binary search, but I can't get it to work. Someone please help!!
    My code so far is this:
    Java Code:
    package sorting;
    
    import java.lang.*;
    import java.util.*;
    
    public class sorteh {
    	public static void main(String [] args){
    		int[] array=new int [20]; //creates new array
    		for (int x=0;x<array.length;x++){   //populates array
    			array[x]=x*3+1;
    		}
    		int[] sorted=bubble(array); //sorts array
    		System.out.println("What number do you want to search for?");
    		Scanner usrin=new Scanner(System.in);
    		int search=usrin.nextInt();
    		System.out.println(Arrays.toString(sorted));
    		int end=search(sorted,search);
    		System.out.println(end);
    		
    	}
    
    	static int[] bubble(int[] in){ //sorts array
    		for (int x=0;x<in.length;x++){
    			for (int y=0;y<in.length;y++){
    				if (in[x]<in[y]){
    					int holding=in[x];
    					in[x]=in[y];
    					in[y]=holding;
    				}
    				}
    			}
    		return in;
    		}
    	
    	static int search(int[] in,int search){
    		int high=in.length-1;
    		int low=0;
    		int mid=(high+low)/2;
    		System.out.println("Initial mid is "+mid);
    		mid=(high+low)/2;
    		while (low<=high){	
    			System.out.println("Mid is "+mid);
    			if (search==in[mid]){ //mid matches search
    				return mid;}
    			else;
    				if (search<in[mid]){
    					high=mid-1;
    					System.out.println("lowered high to "+high);}
    				else;{
    					low=mid+1;
    					System.out.println("hightened low to "+low);}
    			}
    		return -1;
    		}
    	}
    Here is what the console says:
    What number do you want to search for?
    4 //this was my input
    [1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49, 52, 55, 58]
    Initial mid is 9
    Mid is 9
    lowered high to 8
    hightened low to 10
    -1

    I copied what a website did for the sorting part, but if I have low=0 and high=19, wouldn't mid not be an int? What am I missing?
    Thank you in advance.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,410
    Rep Power
    25

    Default Re: Binary search

    Suggestion for the debug output: include the array's contents at the new index's location.

    The use of {}s is not consistent and makes the code hard to read.
    There are several hidden }s at the end of statements. }s should be on their own line.
    else statement are missing the {}s to enclose the statements controlled by the else.
    Last edited by Norm; 07-10-2014 at 04:28 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jProtagonist is offline Member
    Join Date
    Jul 2014
    Posts
    7
    Rep Power
    0

    Default Re: Binary search

    Quote Originally Posted by Norm View Post
    Suggestion for the debug output: include the array's contents at the new index's location.
    Is this what you mean?

    Java Code:
    static int search(int[] in,int search){
    		int high=in.length-1;
    		int low=0;
    		int mid=(high+low)/2;
    		System.out.println("Initial mid is "+mid);
    		mid=(high+low)/2;
    		while (low<=high){
    			System.out.println("Mid is "+mid);
    			if (search==in[mid]){ //mid matches search
    				return mid;
    			}
    			else;{
    				if (search<in[mid]){
    					high=mid-1;
    					System.out.println("lowered high to "+high);
    					System.out.println("Array is "+Arrays.toString(in));
    				}
    				else;{
    					low=mid+1;
    					System.out.println("hightened low to "+low);
    					System.out.println("Array is "+Arrays.toString(in));
    				}
    			}
    		}
    		return -1;
    	}

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,410
    Rep Power
    25

    Default Re: Binary search

    You forgot to post the output that shows what is being printed.

    If the array's contents doesn't change, there is no need to keep printing it.

    the array's contents at the new index's location. I suggested printing this: in[theIndexValue]
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,609
    Rep Power
    5

    Default Re: Binary search

    Would you change "else;{" to "else {"

    It is unnecessary and makes the code harder to read.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 1
    Last Post: 12-01-2012, 03:20 AM
  2. sequential search and binary search
    By qaseh in forum New To Java
    Replies: 2
    Last Post: 03-15-2012, 06:11 AM
  3. Binary Search
    By cengho in forum Java Applets
    Replies: 4
    Last Post: 12-24-2010, 10:26 AM
  4. Binary Search Help
    By Plissken in forum New To Java
    Replies: 2
    Last Post: 03-13-2010, 10:34 AM
  5. Binary search tree search method
    By chopo1980 in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 01:42 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •