What's wrong with this code?

Howard

Lifer
Oct 14, 1999
47,982
11
81
public class Mergesort
{
public static void main (String [] args) throws IOException
{
BufferedReader input1, input2;
PrintWriter output;

String line;
Vector olddata = new Vector (1, 1);
Vector newdata = new Vector (1, 1);
Vector merged = new Vector (1, 1);

int index = 0;
int length = 0;

input1 = new BufferedReader (new FileReader ("phone.txt"));
line = input1.readLine ();
while (line != null)
{
olddata.addElement (line);
line = input1.readLine ();
length++;
}
input1.close ();
length = 0;
input2 = new BufferedReader (new FileReader ("customer.txt"));
line = input2.readLine ();
while (line != null)
{
newdata.addElement (line);
line = input2.readLine ();
length++;
}
input2.close ();
for (int i = 0 ; i < olddata.size () ; i++)
{
merged.addElement (olddata.elementAt (i));
}
for (int i = 0 ; i < newdata.size () ; i++)
{
merged.addElement (newdata.elementAt (i));
}
for (int x = 0 ; x < merged.size () ; x++)
{
int smallest = -1;
for (int y = x + 1 ; y < merged.size () ; y++)
{
if ((((String) merged.elementAt (y)).toLowerCase ()).compareTo (((String) merged.elementAt (x)).toLowerCase ()) < 0)
{
smallest = y;
}
}
if (smallest != -1)
{
merged.insertElementAt ((String) merged.elementAt (smallest), x);
merged.removeElementAt (smallest + 1);
merged.trimToSize ();
}
}
output = new PrintWriter (new FileWriter ("current.txt"));
for (int i = 0 ; i < merged.size () ; i++)
{
output.println (merged.elementAt (i));
}
output.close ();
}
}

Basically I read two text files into two vectors, merge the vectors together, and sort them. I think the sorting algorithm is wrong, although for my last program it worked (I transplanted it). Anybody care to help?

Hope the formatting comes out OK...
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Umm, how about you at least give us an idea what's wrong rather than make us debug all your code for you? :)
 

Flyermax2k3

Diamond Member
Mar 1, 2003
3,204
0
0
what's the error and what's the compiler? Don't make me copy and paste this and do all your work for you
rolleye.gif
 

Yax

Platinum Member
Feb 11, 2003
2,866
0
0
This otta fix it:

in length = 12;

input1 = length++ d!ck
Vector merged => (_(|)_) C==(_)_)

merged.insertElementAt ((String) merged.elementAt (smallest), x); = uh Uh UH AHHH!

output.close (|);

;)
 

AgentEL

Golden Member
Jun 25, 2001
1,327
0
0
You are doing a Mergesort correct?

That involves a recursive call.

In general, if you have two Vectors sorted in ascending order. You simply take the smallest of the two front elements and put it into the merged Vector. Bam, you're done.

The problem is that you have to have two _sorted_ Vectors. But, that's why you call Mergesort recursively until you do have two sorted Vectors. I don't really see that in your code.
 

eLiu

Diamond Member
Jun 4, 2001
6,407
1
0
Originally posted by: cheapbidder01
This otta fix it:

in length = 12;

input1 = length++ d!ck
Vector merged => (_(|)_) C==(_)_)

merged.insertElementAt ((String) merged.elementAt (smallest), x); = uh Uh UH AHHH!

output.close (|);

;)

LOL