Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8017181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:46:01+00:00 2026-06-04T20:46:01+00:00

Now I am in grade 12 at high school taking a programming course in

  • 0

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 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:46:03+00:00Added an answer on June 4, 2026 at 8:46 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to grade a quiz application I would like to make. I have
I am taking an assembly course now, and the guy who checks our home
I am current learning Flex 4 programming now and trying to get as much
I have 2 table TABLE CLASS ( school varchar(50), year varchar(50), grade varchar(50), className
I have a global array in javascript say jsonArr = [location,department,grade]; now inside my
Now I'm sure this is super easy, I have a function called remove_deposit which
I have a table named Grades which contains 2 columns: Name and Grade. I
I have a JOptionPane popup in my applet normally, a-la: Object[] options = {Grade,
So I have a table as follows: ID_STUDENT | ID_CLASS | GRADE ----------------------------- 1
What i am trying to do is a simple recommender , must take the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.