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

Posted on 2007-08-11
Last Modified: 2013-12-29
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;
                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
Question by:deomega22
    LVL 12

    Expert Comment

    "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

    So, i am assuming you are doing this as a learning exercise.

    Author Comment

    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?
    LVL 12

    Accepted Solution

    Take a look at the algorithm here:

    Bubble Sorts

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


    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now