GeeCON Prague 2014*
The moose likes Java in General and the fly likes Binarysearch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Binarysearch" Watch "Binarysearch" New topic
Author

Binarysearch

Anant Rao
Ranch Hand

Joined: Nov 12, 2004
Posts: 126
i have simple problem with binarysearch using arrays

Let p refer to first item in list 1, and r refer to first item in list 2
while more elements in list 1
look for item at position p of list1 at or after position r in list 2
if item found at or after position r in list 2
set r to that position *** how to set the postion in arry so that for the next iteration it will start from that position
increment P
else
increment R
look for item at position p before position r in list 2*** *** how to set the postion in array and it searchs from the postion before r

if found set r to that position
advance p to next position
increment N

help is appreciated
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

"infyniti"-
Welcome to the JavaRanch! We like to keep a professional lookin' image around here, and don't really fancy it when people try to show up the one-eyed moose. So if you could change your display name (click here), that'd be just dandy. Basically, it should be a believable first AND last name (If you'd like a more thorough reason as to why I'm asking you to change your name, check out the Naming Policy.)

Also, we don't like to do people's homework for them. But we're more than willing to help you get over specific issues that you're having. What code have you tried to use that didn't work? What problem were you experiencing? Try posting a code snippet and we'll see what we can do.

Lastly, I'm moving this to the intermediate forum where I think its a better fit. So please post your replies there.

Thanks! And again, Welcome to the JavaRanch!
Anant Rao
Ranch Hand

Joined: Nov 12, 2004
Posts: 126
Thanks Jessica Sant for moving my post to right place...

And i have worked it ....the only problem i am not able to set the postion of that r as explained in my previous post so that the next iteration will start from there...so i just if know if any method exists of moving to certain to location...

thanks
infynit (fyi my name is infyniti)
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Check out the naming policy once more --> Basically, it should be a believable first AND last name.

As for your problem.

Why not just keep two integers that act as pointers to a position in the array?
Anant Rao
Ranch Hand

Joined: Nov 12, 2004
Posts: 126
Thanks Jessican sant ...I am pasting my code can u just let me know if i want to start my next iteration of sFile2 from the last found postion of list instead of searching from the first...
FileReader frFile1 =
new FileReader("file1");
BufferedReader brFile1 = new BufferedReader(frFile2);

FileReader frFile2 =
new FileReader(File2");
BufferedReader brFile2 = new BufferedReader(frFile2);

String sFile1;
String sFile2;
int index;

ArrayList al = new ArrayList(10000);

// add elements of Bennie to the array list sBennie
while ((sFile1 = brFile1.readLine()) != null) {
al.add(sFile1);
}

while ((sFile2 = brFile2.readLine()) != null) {

//while (brQueries.readLine() != null) {
// Search
index = Collections.binarySearch(al, sFile2);

if (index >= 0) {
System.out.println(sFile2 + " y");
} else {
System.out.println(sFile2 + " n");
}
}

and ur right i will surely change my name by adding my First name
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Binarysearch