Hi, I had an assignment where we had to fox a program and get it working correctly. I fixed 99% of the problems and the program runs correct. The only thing that is wrong is my insertionSort() method. It runs correctly, but in the wrong order.

Currently its listing names in descending order, comparing the last names (if last names are equal then compares first names). I have been messing around with my loops for a while now and cant figure out how to switch the order which it runs the sort. The fix is probably something really small and obvious that I'm missing.

    public static void insertionSort(Person[] personArray) {
        int i, j;

        for (j = 1; j < personArray.length; j++) {
          Person temp = personArray[j];
          i = j;

          while (j > 0 && personArray[j - 1].compare(temp) < 0 ) {
            personArray[j] = personArray[j - 1];
            j--;
          }
          personArray[j] = temp;
        }
      }



    public static void main(String[] args) {
        System.out.println("Hello and Good Luck with your Debugging Exercise");

        // initialize an array of comparable objects
        Person[] array = new Person[5];
        array[0] = new Person("Edison", "Zach");
        array[1] = new Person("Clarkson", "Happy");
        array[2] = new Person("Edison", "Thomas");
        array[3] = new Person("John", "Doe");
        array[4] = new Person("Allen", "Woody");

        Lab6.insertionSort(array);

        for (Person p : array) {
            System.out.println(p);
        }
    }
}

Output from this code is currently:

  • Doe John
  • Zach Edison
  • Thomas Edison
  • Happy Clarkson
  • Woody Allen

Should be:

  • Woody Allen
  • Happy Clarkson
  • Thomas Edison
  • Zach Edison
  • John Doe

Dani AI

Generated

is spot on: your inner loop is written for a descending pass. In insertion sort, the inner loop shifts elements to the right while the element on the left is greater than the key for ascending order. Flipping the comparison (or swapping the operands) fixes the direction. Also, you assign i = j but never use i, which makes the indexing harder to follow. Either drop i or use it as the inner index so you are not mutating the loop counter you are iterating with.

A clear ascending-order inner loop looks like this:

// shift larger items right until the correct slot for 'key'
while (pos > 0 && a[pos - 1].compare(key) > 0) {
    a[pos] = a[pos - 1];
    pos--;
}
a[pos] = key;

That > 0 says: while the item to the left should come after key, keep shifting. Alternatively, keep your original sign and swap operands: while (pos > 0 && key.compare(a[pos - 1]) < 0) { ... }. Either way, you end up with Allen, Clarkson, Edison(Thomas), Edison(Zach), John as desired.

Quick sanity checks before/after the change:

  • If last names are equal, ensure your compare(...) checks first names and returns a negative when the left first name should come before the right.
  • Verify stability expectations: classic insertion sort is stable if your comparison only shifts when strictly greater. Using >= 0 would break stability by moving equals.

If this were not a sorting-algorithm exercise, Arrays.sort with a comparator is simpler and self-documenting, but for this assignment , your logic is correct once that comparison is adjusted.

Recommended Answers

All 2 Replies

Line 8, try to change personArray[j - 1].compare(temp) < 0 to personArray[j - 1].compare(temp) > 0.

Well I feel pretty stupid, I knew it was something simple like that which I just wasnt seeing after staring at the same code for too long. Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.