我有一个简单的递归合并排序,我只是尝试对实现Comparable的整数数组进行排序。我不知道为什么我得到一个错误,当它运行时它打印出我创建的随机整数的ArrayList然后打印
还没有错误
还没有错误
线程“main”中的异常java.lang.StackOverflowError
然后重复
在MergeTemplate.rMerge(MergeTemplate.java:38)
一直到最后都说
处理完成
import java.util.*;
public class MergeTemplate{
private ArrayList <Comparable> temp1=new <Comparable> ArrayList();
int num;
Random ar=new Random();
public MergeTemplate(){
num=25;
}
public MergeTemplate(int n){
num=n;
}
public ArrayList <Comparable> fillArray(){
ArrayList <Comparable> ar1=new <Comparable> ArrayList();
for(int i=0;i<num; i++)
ar1.add(ar.nextInt(11));
screenOutput(ar1);
return ar1;
}
public void screenOutput(){
for(Comparable x: temp1)
System.out.print(x+ " ");
System.out.println();
}
public void screenOutput(ArrayList <Comparable> temp){
for(Comparable x: temp)
System.out.print(x+ " ");
System.out.println();
}
public void rMerge(ArrayList <Comparable> rList){
rMerge(rList, 0, rList.size()-1);
}
public void rMerge(ArrayList <Comparable> rList, int first, int last){
if (first-last==0){
System.out.println("no error yet");
}
else{
rMerge(rList, first, last/2);
rMerge(rList, last/2 + 1, last);
merge(rList, first, last);
}
}
public void merge(ArrayList <Comparable> a, int first, int last){
Comparable placeHolder;
if(a.get(first).compareTo(a.get(last))>1){
placeHolder=a.get(first);
a.set(first, a.get(last));
a.set(last, placeHolder);
}
}
}
public class Tester {
public static void main(String[] args) {
MergeTemplate one=new MergeTemplate(8);
one.rMerge(one.fillArray());
}
}
答案 0 :(得分:0)
您的合并方法有错误,它不会遍历所有列表以验证订单结果。
答案 1 :(得分:0)
我认为你应该在你的病情中添加(最后&lt;第一):
if (first-last==0 && last < first){
System.out.println("no error yet");
}
答案 2 :(得分:0)
您的错误发生在
中rMerge(rList, first, last/2);
当第一个等于2时,最后一个小于2,实际上最后等于0并继续调用自身。您需要检查是否有1个元素。
将未排序的列表分成n个子列表,每个子列表包含1个元素(1个元素的列表被视为已排序)
- 醇>
重复合并子列表以生成新的已排序子列表,直到只剩下1个子列表。这将是排序列表。
来自维基百科。
答案 3 :(得分:0)
您需要多一个键来表示分割点以适当地划分元素。还需要重新实现merge()方法以迭代所有目标元素。 完整的代码,带有rMerge()方法的实现,添加一个名为mid的键和merge()方法的实现,如下所示。
import java.util.ArrayList;
import java.util.Random;
public class MergeTemplate {
private Random random = new Random();
private ArrayList<Comparable> temp1 = new ArrayList<>();
private int num;
public MergeTemplate() {
this.num = 25;
}
public MergeTemplate(int num) {
this.num = num;
}
public ArrayList<Comparable> fillArray() {
ArrayList<Comparable> ar1 = new ArrayList<>();
for (int i = 0; i < num; i++) {
ar1.add(random.nextInt(11));
}
screenOutput(ar1);
return ar1;
}
public void screeOutput() {
for (Comparable x : temp1) {
System.out.println(x + " ");
}
System.out.println();
}
public void screenOutput(ArrayList<Comparable> temp) {
for (Comparable x : temp) {
System.out.println(x + " ");
}
System.out.println();
}
public void rMerge(ArrayList<Comparable> rList) {
rMerge(rList, 0, rList.size() - 1);
}
public void rMerge(ArrayList<Comparable> rList, int first, int last) {
int mid = 0;
if (first >= last) {
System.out.println("no error yet");
} else {
mid = (first + last) / 2;
rMerge(rList, first, mid);
rMerge(rList, mid + 1, last);
merge(rList, first, mid, last);
}
}
public void merge(ArrayList<Comparable> a, int first, int mid, int last) {
int i, j, k, m;
i = first;
j = mid + 1;
k = first;
ArrayList<Comparable> tempList = new ArrayList<>();
// compare two divided parts
while (i <= mid && j <= last) {
if (a.get(i).compareTo(a.get(j)) < 1) {
tempList.add(a.get(i));
i++;
} else {
tempList.add(a.get(j));
j++;
}
k++;
}
if (i > mid) {
for (m = j; m <= last; m++) {
tempList.add(a.get(m));
k++;
}
} else {
for (m = i; m <= mid; m++) {
tempList.add(a.get(m));
k++;
}
}
for (i = 0, j = first; i < tempList.size(); i++, j++) {
a.set(j, tempList.get(i));
}
}
}