2

I need help sorting an integer array using selection sort. It won't sort for some reaons. Below is my demo/main.

  02 
  20 
  01 

it should be

  01 
  02 
  20

My demo/main:

    public static void main(String[] args) {


    SelectionSortArray[] ints = new SelectionSortArray[3];

    ints [0] = new SelectionSortArray(02);
    ints [1] = new SelectionSortArray(20);
    ints [2] = new SelectionSortArray(01);

    System.out.println("Unsorted array: ");

    for (int index = 0; index < ints.length; index++) {
        System.out.println(ints[index]);
    }


    SelectionSort.selectionSort(ints);

    System.out.println(" ");

    System.out.println("Sorted array using selection sort: ");

    for (int index = 0; index < ints.length; index++) {
        System.out.println(ints[index]);
    }


}
5
  • Why you are not using Collections.sort() method? Commented Aug 29, 2013 at 17:08
  • @Sanbhat it's probably homework, so collections.sort is probably out of the question Commented Aug 29, 2013 at 17:09
  • @sanbhat I need to sort the integer array using the SelectionSort class/method. Commented Aug 29, 2013 at 17:09
  • @sanbhat because Collections.sort() doesn't work with Arrays . @user2256002 see Arrays.sort() when you have spare time to get to know other way to efficiently sort (with 1.7 it uses DualPivotQuicksort internally ) Commented Aug 29, 2013 at 18:02
  • 1
    This question appears to be off-topic because it is about reviewing and debugging a specific fragment of code. Commented Sep 1, 2013 at 16:45

3 Answers 3

2

Your compareTo method in the SelectionSortArray class is incorrect. The compareTo method must return an int less than zero if the current object is less than the other object, yet you have it returning 1.

Quoting from the linked Javadocs:

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Try these changes:

if (num == other.num) {
    result = 0;   // This was correct; no change here.
} else if (num < other.num) {
    result = -1;  // Changed from 1 to -1.
} else {
    result = 1;   // 1 or 2 is fine, as long as it's positive
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your CompareTo function is wrong. Just change it to:

public int compareTo(SelectionSortArray other) {
    return num.compareTo(other.num);     
}

The key is the -1 / 0 / 1 return codes, rather than the "0,1,2" that you're using.

Generally if you're comparing built in types, it's easier to just delegate to the built in comparison operators.

Note: To use "num.compareTo" you need to use "Integer" rather than "int". If you want to stick with "int", you need the solution posted by rgettman.

Comments

0

Note that along with the changes to compareTo,

return Integer.compare(this.num,other.num)

Which is implemented to return what you are returning but in a concise way

public static int compare(int x, int y) {
    return (x < y) ? -1 : ((x == y) ? 0 : 1);
}

if you want to see the printed output in the way you listed in your question use

System.out.format("%02d%n",ints[index].num);

or alter toString() to return String.format("%02d",num)

You might also want to see java.util.Arrays source code so see how few other similar methods have been implemented. Also consider changing your class name to indicate that it holds number CustomNumber and encapsulate your class to avoid direct access to num.

And another Interesting catch in your code

    SelectionSortArray[] ints = new SelectionSortArray[8];
    ints[0] = new SelectionSortArray(01);
    ints[1] = new SelectionSortArray(02);
    ints[3] = new SelectionSortArray(03);
    ints[4] = new SelectionSortArray(04);
    ints[5] = new SelectionSortArray(05);
    ints[6] = new SelectionSortArray(06);
    ints[7] = new SelectionSortArray(07);
    ints[8] = new SelectionSortArray(08);//???

if you precede a number with 0 it will be an Octal number and allowed digits are 0 - 7 hence you will see compilation error above. Also

    System.out.println(0123);
    System.out.println(0234);

will not print

0123
0234

as you (un)expected !

83
156

Just be cautious while using octal numbers in your code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.