Jump to content

Binary Search Help

- - - - -

  • Please log in to reply
6 replies to this topic

#1
OwsumEmam

OwsumEmam

    Newbie

  • Members
  • Pip
  • 7 posts
I did the program of Binary Search but got stuck somewhere...Please Help me on this.

class BinarySearch
{
public static void main(String[] args)
{
int data[] = {9,11,23,43,54,66,76,78,99};
int beg = data[0];
int end = data[data.length-1];
int mid = (int)((end-beg)/2);
int item = 78;
int loc = 0;
do
{
if(item<data[mid])
{
end = mid-1;
}
else
beg = mid+1;
mid = (int)((end-beg)/2);
}
while(beg<=end && data[mid]!=item);

if(data[mid]==item)
System.out.println("Successful");
else
System.out.println("Unsuccessful");
}
}

#2
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
You should do:

OwsumEmam said:

int beg = 0;
int end = data.length-1;

I didn't read the rest of the program but these two lines were wrong

#3
margusmartsepp

margusmartsepp

    Newbie

  • Members
  • Pip
  • 2 posts
Or you could just use:

		int data[] = { 9, 11, 23, 43, 54, 66, 76, 78, 99 };

		int item = 78, location = java.util.Arrays.binarySearch(data, item);

		System.out.println(location);


#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

margusmartsepp said:

Or you could just use:

		int data[] = { 9, 11, 23, 43, 54, 66, 76, 78, 99 };

		int item = 78, location = java.util.Arrays.binarySearch(data, item);

		System.out.println(location);

Some users are trying to learn how the gears turn inside of these methods :)

At the OP, I see you're using another do...while implementation of a search. I think you might be making your algorithm harder to understand than it should be. You can make it easier to understand by using a while loop or recursion.
If this is a class assignment, then pardon my suggestion :)

The reason I say this is because I see you're still missing some standard tests, similar to your linear search with a do...while.

#5
OwsumEmam

OwsumEmam

    Newbie

  • Members
  • Pip
  • 7 posts
Thanks for your precious reply but dear I am not supposed to use any of the packages...like you did....I have to make a simple program but I get stuck in indexing the arrays. Can you help me out or anyone from you. Thanks

#6
OwsumEmam

OwsumEmam

    Newbie

  • Members
  • Pip
  • 7 posts
Hey eafkuor....can you please help me out of this like you did before for me in Linear Search...It's really not that I just try first time and just give up early...I try but can't succeed than post here. :) Help me bro!

#7
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
public class JavaClass {

	public static void main(String[] args){

		int data[] = {9,11,23,43,54,66,76,78,99};

		int position;

		

		//let's try to see if the binary search works for every number in the data array

		for(int i=0;i<data.length;i++){

			position=binSearch(data, data[i]);

			if(position!=-1){

				System.out.println("The element " + data[i] + " is at position " + position);

			} else {

				System.out.println("Element "+ data[i] +" not in the array");

			}

		}

		

		

		//let's try with a couple of numbers that are not in the data array

		position=binSearch(data, 5);

		if(position!=-1){

			System.out.println("The element " + 5 + " is at position " + position);

		} else {

			System.out.println("Element "+ 5 +" not in the array");

		}

		

		position=binSearch(data, 1000);

		if(position!=-1){

			System.out.println("The element " + 1000 + " is at position " + position);

		} else {

			System.out.println("Element "+ 1000 +" not in the array");

		}


	

	}

	

	public static int binSearch(int [] array, int elem){

		int beg = 0;

		int end = array.length-1;

		int mid = (end+beg)/2;

		

		while(beg<mid && mid<end){

			if(array[mid]==elem)

				return mid;

			

			if(array[mid]<elem){

				beg=mid;

			} else {

				end=mid;

			}

			

			mid = (end+beg)/2;

		}

		

		if(array[beg]==elem)return beg;

		if(array[end]==elem)return end;

		

		return -1;

	}

}





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users