3 Replies - 639 Views - Last Post: 12 November 2010 - 02:38 AM Rate Topic: -----

#1 Sminkah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-November 10

Binary Search for Strings

Posted 11 November 2010 - 04:23 PM

I've created an object Reservation that contains information about customers, first name, last name, address, etc.
I've created an array to store these objects.
My problem is, i'm trying to use a binary search to look for the last names of the customers (which have been sorted) and output the information associated. From what I can see, it DOES find the customer with a given last name, but it doesn't stop outputting the information associated..

public void binarySearch(String key){

		int low = 0;
		int high = a.length - 1;
		while (low <= high){

			int mid = (low + high) / 2;
			int cmp = a[mid].getLastName().compareTo(key);
			if (cmp == 0)
				System.out.println(a[mid]);
			else if (cmp < 0)
				low = mid + 1;
			else
				high = mid - 1;
		}
		

	}



Anyone have any ideas as to what's wrong here?

Is This A Good Question/Topic? 0
  • +

Replies To: Binary Search for Strings

#2 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Binary Search for Strings

Posted 11 November 2010 - 04:33 PM

place a break statement after the println.
This will kick you out of the current loop (in ur case it will jump to the end of the while loop) after you println.

if( cmp == 0 )
{
  System.out.println(a[mid]);
  break;
}


This post has been edited by SwiftStriker00: 11 November 2010 - 04:35 PM

Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,596
  • Joined: 27-December 08

Re: Binary Search for Strings

Posted 11 November 2010 - 04:38 PM

You could also use a return statement rather than a break statement to return control from the method. Better practice though would be to return the Object rather than printing it, allowing your method to be more modular and reusable to other components. This means you would have to change the return type of your binarySearch() method.
Was This Post Helpful? 1
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,652
  • Joined: 20-September 08

Re: Binary Search for Strings

Posted 12 November 2010 - 02:38 AM

macosxnerd101 is right. Here is one way of doing it (change Object to the appropriate type)

    public Object binarySearch(String key) {
        Object result = null;
        int low = 0;
        int high = a.length - 1;

        while ((low <= high) && (result == null)) {
            int mid = (low + high) / 2;
            int cmp = a[mid].getLastName().compareTo(key);

            if (cmp == 0) {
                result = a[mid];
            } else if (cmp < 0) {
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }

        return result;
    }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1