Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Help Need To Get Some Online Information (http://www.programmingforums.org/showthread.php?t=1460)

Infyniti Dec 7th, 2004 5:38 PM

I have two files with ordered list of names and i have to copy them to array list which i have done already. I have use binary search to compare them.

Can anybody suggest me any online information how I can use binarysearch on strings in arraylist.


thanks in advance.

Mjordan2nd Dec 7th, 2004 7:15 PM

What exactly do you need help with?

You could check the javadocs for arraylists, though, if you need some help. Also, check the collections class, which has some useful sort methods if you haven't already.

Wouldn't a binary search on an arraylist be similar to a binary search for an array?

Infyniti Dec 8th, 2004 12:44 PM

Arraylist1= m1,m2,m3,m4,m5
arraylist2= m6 m5,m1,m7,m2,m8

This is the pseudo-code for my algorithm
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
increment P
else
increment R
look for item at position p before position r in list 2
if found set r to that position
advance p to next position

I have to do this using binarysearch...but for binarysearch the elments have to be sorted. If i sort the positions of elements gets changed . Can u suggest where i am going wrong....is that possible to do using binarysearch.

just an idea will be helpful

regards
infyniti

ZenMasterJG Dec 9th, 2004 8:50 AM

Yes, to use a binary search your lists must be sorted. Why is it a problem that they'll be at different indexes in the arraylist? I just don't really understand what your problem is...

Infyniti Dec 9th, 2004 10:10 AM

Thanks for the reply...I am posting my code for the algorthim provided in my previous positng using linkedlist nothing related to binaryserch. I am unable to figure out where i am going wrong...can u help me out

The linked list contains
listA= m1,m2,m3,m4,m5
listB= m6,m5,m1,m7,m2,m8

package com.my.test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.LinkedList;
import java.util.List;

class Linkedlist {
public static void main(String args[]) throws Exception {
long startTime = System.currentTimeMillis();

FileReader frListB =
new FileReader("C:/Documents and Settings/Anjana/My Documents/doc/Second.txt");
BufferedReader brListB = new BufferedReader(frListB);

FileReader frListA =
new FileReader("C:/Documents and Settings/Anjana/My Documents/doc/First.txt");
BufferedReader brListA = new BufferedReader(frListA);

List listA = new LinkedList();
List listB = new LinkedList();
String sListB;
String sListA;
int position = 0;
boolean flag = false;

//Copying the contents from second.txt into an LinkedList
while ((sListB = brListB.readLine()) != null) {
listB.add(sListB);

}

while ((sListA = brListA.readLine()) != null) {
System.out.println("sListA =" + sListA);
for (int i = position; i < listB.size(); i++) {
System.out.println("listb.get(i)= " + listB.get(i));
if (sListA.equals(listB.get(i))) {
position = i + 1;
System.out.println("position =" + position);
flag = false;
break;
} else {
flag = true;
}
}

if (flag == true) {
System.out.println("sListA =" + sListA);
for (int i = position-1 ; i>=0; i--) {
System.out.println("second listb.get(i)= " + listB.get(i));

if (sListA.equals(listB.get(i))) {
position = i - 1;
} else {
flag = false;
}
}

}

}

}
}Thanks in advance
Infyniti

Infyniti Dec 9th, 2004 2:18 PM

I am done with the code.... and thanku for the help.

infyniti


All times are GMT -5. The time now is 3:35 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC