Solved

# bubblesort an array of objects using a comapreTo() and a bubbleSort()

Posted on 2007-08-11
Medium Priority
581 Views
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).competitorMark)
return -1;
else if (this.competitorMark == ((Competitor)o).competitorMark)
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[current + 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
0
Question by:deomega22
• 2

LVL 12

Expert Comment

ID: 19677897
"current+1"  goes beyond the array size.
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.
0

Author Comment

ID: 19677923
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?
0

LVL 12

Accepted Solution

marklorenz earned 1500 total points
ID: 19677933
Take a look at the algorithm here:

Bubble Sorts
http://www.leepoint.net/notes-java/data/arrays/32arraybubblesort.html

You'll see that they don't index the way you are doing.  I'd suggest you follow their lead.

Mark
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.
###### Suggested Courses
Course of the Month17 days, 8 hours left to enroll