I am having trouble with converting my sorting class to sort objects with arraylists. It is currently sorting objects, but I am having trouble converting it to sorting Arraylists. Here is the code:
package Merge_Sort_Objects_ArrayList;
import java.util.ArrayList;
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(ArrayList <Comparable> items, int start, int mid, int end){
Comparable temp;
int pos1 = start;
int pos2 = mid + 1;
int spot = start;
ArrayList <Comparable> objectSort = items;
while (!(pos1 > mid && pos2 > end)){
if ((pos1 > mid) || ((pos2 <= end) &&(items[pos2].getRadius() < items[pos1].getRadius()))){
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(ArrayList <Comparable> 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);
}
}
}
now I have started to convert it, however I am stuck on this section right here:
while (!(pos1 > mid && pos2 > end)){
if ((pos1 > mid) || ((pos2 <= end) &&(items[pos2].getRadius() < items[pos1].getRadius()))){
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];
}
Thank you in advance!
Listat all before, here's a tutorial that might be of some help: docs.oracle.com/javase/tutorial/collections/interfaces/…. The first step is learning the difference between a list and an array.