deomega22
asked on
bubblesort an array of objects using a comapreTo() and a bubbleSort()
doing a bubblesort... stupid i know. anyway i got a compareTo(Object o) method going on, getting the -1, 1, 0 values and passing them back to a bubbleSort() for all the swapping. I am fairly familiar with java, but not with this kinda of sorting. normally I would just use a java library sorting technique.
just want to know what wrong with this. i'm getting a nullpointerexception, but not sure why since i know for a fact that the object is not null. all I have to do is sort the "mark" highest to lowest. I am not at all concerned with O notation.
public int compareTo(Object o) {
if (this.competitorMark < ((Competitor)o).competitor Mark)
return -1;
else if (this.competitorMark == ((Competitor)o).competitor Mark)
return 0;
else
return +1;
}
public static Competitor[] bubbleSort(Competitor[] a) {
boolean swappedOnPrevRun = true;
while (swappedOnPrevRun) {
swappedOnPrevRun = false;
for (int i = 0; i < a.length - 1; i++) {
int current = i;
if (a[current].compareTo(a[cu rrent + 1]) == 1)
swappedOnPrevRun = true;
Competitor temp = a[i];
a[current] = a[current + 1];
a[current + 1] = temp;
}
}
return a;
}
this is what is being passed to it (id, name, mark)
6241 Cathy 8.53
3792 Patrick 8.25
2910 John 7.32
8371 Mary 9.72
9372 Tony 9.12
3532 Jane 6.37
just want to know what wrong with this. i'm getting a nullpointerexception, but not sure why since i know for a fact that the object is not null. all I have to do is sort the "mark" highest to lowest. I am not at all concerned with O notation.
public int compareTo(Object o) {
if (this.competitorMark < ((Competitor)o).competitor
return -1;
else if (this.competitorMark == ((Competitor)o).competitor
return 0;
else
return +1;
}
public static Competitor[] bubbleSort(Competitor[] a) {
boolean swappedOnPrevRun = true;
while (swappedOnPrevRun) {
swappedOnPrevRun = false;
for (int i = 0; i < a.length - 1; i++) {
int current = i;
if (a[current].compareTo(a[cu
swappedOnPrevRun = true;
Competitor temp = a[i];
a[current] = a[current + 1];
a[current + 1] = temp;
}
}
return a;
}
this is what is being passed to it (id, name, mark)
6241 Cathy 8.53
3792 Patrick 8.25
2910 John 7.32
8371 Mary 9.72
9372 Tony 9.12
3532 Jane 6.37
ASKER
yea.. learning. if i could use array.sort i would in a heart beat.
i see what you are saying about current + 1 is out of bounds, so i tried i put the for loop to be i < a.length -1.
is my approach all wrong?
i see what you are saying about current + 1 is out of bounds, so i tried i put the for loop to be i < a.length -1.
is my approach all wrong?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In your example data, the array index goes from 0 to 5. When your for loop gets to length-1 (5), current+1 = 6, which is beyond your array size. Remember, arrays don't automatically grow like Collection subclasses.
and you are correct, there are better ways than writing your own, see here:
Sorting Arrays
http://www.leepoint.net/notes-java/data/arrays/70sorting.html
So, i am assuming you are doing this as a learning exercise.