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?