2

Now I am in grade 12 at high school taking a programming course in Java. Now I have a test tomorrow, and I am trying to study. The test is a practical test, and I have to take either primitive data or an object and sort them using Insertion Sort, Selection Sort, and Merge sort into either primitive data, an array, or an array list. Now I have it bang down on primitive data. It's just that I am having a really hard time on sorting objects into arrays and array lists. I have selection sort down for arrays and array list, so it is just Merge sort and Insertions sort I am having dear trouble with. I just don't get how to do it.

Thank you for your help in advance.

To simplify the question, I would like to know how to convert these two classes to sort objects named Circle

My first chunk of code here gets random circles and then feeds it into my other class to sort. My last code block sorts it.

package Merge_Sort_Objects_Array;
import java.util.Scanner;
import java.lang.Math;

public class TestSorts {
    public static void displayArray(int[] array){
        for (int i = 0; i < array.length; i++){
            System.out.print(array[i] + " ");
        }
            System.out.println("\n");
        }

        public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int numItems;
        int[] test;

        System.out.print("Enter number of elements: ");
        numItems = input.nextInt();

        /* populate array with random integers */
        test = new int[numItems];
        for (int i=0; i < test.length; i++){
            test[i] = (int)(100 * Math.random());
        }
        System.out.println("Unsorted:");
        displayArray(test);

        mergesort.mergesort(test, 0, test.length - 1);

        System.out.println("Sorted: ");
        displayArray(test);
    }

}





  package Merge_Sort_Objects_Array;

    public class mergesort {

    /**
     * Merges two sorted portion of items array
     * pre: items[start.mid] is sorted.  items[mid+1.end] sorted.  start <= mid <= end
     * post: items[start.end] is sorted
     */

    private static void merge(int[] items, int start, int mid, int end){
            int[] temp = new int[items.length];
            int pos1 = start;
            int pos2 = mid + 1;
            int spot = start;

            while (!(pos1 > mid && pos2 > end)){
                if ((pos1 > mid) || ((pos2 <= end) &&(items[pos2] < items[pos1]))){
                    temp[spot] = items[pos2];
                    pos2 +=1;
                }else{
                    temp[spot] = items[pos1];
                    pos1 += 1;
                }
                spot += 1;
            }
            /* copy values from temp back to items */

            for (int i = start;  i <= end; i++){
                items[i] = temp[i];
            }
    }

    /**
     * mergesort items[start..end]
     * pre: start > 0, end > 0
     * post: items[start..end] is sorted low to high
     */
    public static void mergesort(int[] items, int start, int end){
        if (start < end){
            int mid = (start + end) / 2;
            mergesort(items, start, mid);
            mergesort(items, mid + 1, end);
            merge(items, start, mid, end);
        }
    }
}

Is that better?

Okay Greg, here is the updated testSorts class. Now I changed them all to Circle instead of int, however I still get that error where I identified earlier.

public class TestSorts {
    public static void displayArray(Circle[] array){
        for (int i = 0; i < array.length; i++){
            System.out.print(array[i] + " ");
        }
            System.out.println("\n");
        }

        public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int numItems;
        Circle[] test;

        System.out.print("Enter number of objects: ");
        numItems = input.nextInt();

        /* populate array with random integers */
        test = new Circle[numItems];
        for (int i=0; i < test.length; i++){
            test[i] = new Circle((int)(10 * Math.random() + 1));
        }
        System.out.println("Unsorted:");
        displayArray(test);

        mergesort.mergesort(test, 0, test.length - 1);

        System.out.println("Sorted: ");
        displayArray(test);
    }

}

Does that help?

1
  • 1
    What's your question? Could you show some code that you're having trouble with? Commented May 31, 2012 at 0:44

1 Answer 1

1

It looks like there are two basic things you need to do to modify your existing sorting code to handle Circle objects.

  1. Change the type int to type Circle in the array type of your sort methods (just the array type, not the indexes).

  2. Do something meaningful in place of items[pos2] < items[pos1] (since you can't compare objects with just <). Perhaps for circles you want to sort by radius, so you could do items[pos2].getRadius() < items[pos1].getRadius(). But that's up to you.

As you learn more about generic programming, you will discover better ways to do this so that your existing code can apply to any suitable type of object without having to modify it each time you need to use it with a new type.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you Greg, you got me 90% of the way there, thank you. I never thought about the .getRadius() idea, that advice truly helps. Another place where I got stuck was at this line near the bottom of testSorts "mergesort.mergesort(test, 0, test.length - 1);" Now, the first test gives me an error saying that it requires an int. Do you know anyway to fix that?
@Spartan: You will also need to modify the code in TestSorts to create an array of Circle objects instead of int.
Yes, I did that, however it still gives me the error. I will update the code at the top to display what I have. But that is why it is confusing me, I did all of that, but yet I still have that darn error.
Never mind, I found the mistake. It was one of those errors that you would overlook. Thank you soo much Greg! You helped a lot, and I finaly understand how to convert sorting classes from primitive to objects.

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.