Wierd Insertion sort problem
For example, say I have a unsorted list of names, while the sorted list should
look like this:
AMBER
AMY
BART
BILL
BOB
CURT
DAN
DAVE
JOHNNY
MARK
MIKE
TOMMY
It comes out something like this
AMBER
AMBER
AMBER
AMBER
AMBER
AMBER
AMBER
AMBER
AMBER
MIKE
AMY
AMY
AMY
AMY
AMY
:confused:
I usually don't have any problems with stuff like this, but for the life of me can't figure out what the heck is wrong with this. Here's the code below. I'll keep trying to figure it out, but if anybody can see something that I'm not seeing with this code, that would be awesome. Thanks a lot. The compares++, and swaps++ are there just to keep track of them.
public static void insertionSort (List data)
{
Comparable tmp;
int i, j;
for(i = 1; i < data.size(); i++)
{
tmp = (Comparable)data.get(i);
for(j = i; (j > 0) && (tmp.compareTo(data.get(j - 1)) < 0)
; j--){
compares++;
data.set(j, data.get(j - 1)) ;
data.set(j, tmp) ;
swaps++;
}
}
}

