Java 方法函数有问题

Java 方法函数有问题,java,arrays,debugging,sorting,Java,Arrays,Debugging,Sorting,因此,代码的基本总体功能是接收数组中的数据并按升序排序。我的代码中没有错误,但我相信有问题。我做了一些测试用例,有些失败了,我感觉在recursivesort方法中我做错了什么。我已经做了多次调试,只是找不到问题出在哪里 public class RecursiveSorter { private int[] sortedArray; private int[] array; public RecursiveSorter() { array = new

因此,代码的基本总体功能是接收数组中的数据并按升序排序。我的代码中没有错误,但我相信有问题。我做了一些测试用例,有些失败了,我感觉在
recursivesort
方法中我做错了什么。我已经做了多次调试,只是找不到问题出在哪里

public class RecursiveSorter {

    private int[] sortedArray;
    private int[] array;

    public RecursiveSorter() {
        array = new int[1];
    }

    public RecursiveSorter(int[] a) {
        array = a;
    }

    public void setArray(int[] a) {
        array = a;
    }

    public int[] getSortedArray() {
        return sortedArray;
    }

    public int[] getOriginalArray() {
        return array;
    }

    public int[] sort() {
        sortedArray = array;
        recursiveSort(sortedArray.length - 1); 
        return sortedArray;
    }

    public int[] recursiveSort(int endIndex) {
        if (endIndex >= 0) {
            int m = getMaxIndex(endIndex, sortedArray);
            swap(m, endIndex, sortedArray);
            recursiveSort(endIndex-1);
        }
        return sortedArray;
    }

    public int getMaxIndex(int endIndex, int[] a) {
        int max = a[0];
        int maxIndex = 0;
        for (int i = 1; i < endIndex; i++) {
            if (a[i] > max) {  
                max = a[i];
                maxIndex = i;
            }
        }
        return maxIndex;
    }

    public void swap(int src, int dest, int[] a) {
        int temp = a[dest];
        a[dest] = src;
        a[src] = temp;
    }

    public String toString() {
        return "Original: " + prettyPrint(getOriginalArray()) + "\n" +
               "Sorted:   " + prettyPrint(getSortedArray());
    }

    private String prettyPrint(int[] a) {
        String s = "";
        for (int i : a)
            s += i + " ";
        return s;
    }

    public static void main(String[] args) {
        // Automate running, but not testing
        int[] array = {5, 67, 12, 20};
        RecursiveSorter s = new RecursiveSorter(array);
        s.sort();
        System.out.println(s); // uses Sorter.toString
    }
}
公共类递归分类器{
达雷私人酒店;
私有int[]数组;
公共递归分类器(){
数组=新整数[1];
}
公共递归分类器(int[]a){
数组=a;
}
公共void集合数组(int[]a){
数组=a;
}
public int[]getSorterDarray(){
返回至Darray;
}
公共int[]getOriginalArray(){
返回数组;
}
公共int[]排序(){
阵列;
递归排序(sortedArray.length-1);
返回至Darray;
}
公共int[]递归排序(int-endIndex){
如果(endIndex>=0){
int m=getMaxIndex(endIndex,sortedDarray);
交换(m、endIndex、SorterDarray);
递归排序(endIndex-1);
}
返回至Darray;
}
public int getMaxIndex(int endIndex,int[]a){
int max=a[0];
int maxIndex=0;
对于(int i=1;imax){
max=a[i];
maxIndex=i;
}
}
返回最大索引;
}
公共无效掉期(int src、int dest、int[]a){
int temp=a[dest];
a[dest]=src;
a[src]=温度;
}
公共字符串toString(){
return“Original:+预打印(getOriginalArray())+”\n+
“排序:”+预打印(getSortedArray());
}
私有字符串预打印(int[]a){
字符串s=“”;
对于(int i:a)
s+=i+“”;
返回s;
}
公共静态void main(字符串[]args){
//自动化运行,但不自动化测试
int[]数组={5,67,12,20};
RecursiveSorter s=新的RecursiveSorter(阵列);
s、 排序();
System.out.println(s);//使用Sorter.toString
}
}
交换方法有错误:

a[dest] = src;
应该是:

a[dest] = a[src];
for (int i = 1; i <= endIndex; i++) {
此外,此行不会复制数组,因此有
array
sortedArray
引用相同的数组对象

sortedArray = array;
替换为:

sortedArray = Arrays.copyOf(array, array.length);
方法
getMaxIndex
中也缺少等号:

for (int i = 1; i < endIndex; i++) {
for(int i=1;i
应该是:

a[dest] = a[src];
for (int i = 1; i <= endIndex; i++) {
for(int i=1;i1-您必须将数组克隆(或复制)到SorterDarray

2-交换方法不正确

3-方法recursiveSort的第一次调用应通过数组的最后一个索引sortedArray.length-1进行参数化

public class RecursiveSorter {

    private int[] sortedArray;
    private int[] array;

    public RecursiveSorter() {
        array = new int[1];
    }

    public RecursiveSorter(int[] a) {
        array = a;
    }

    public void setArray(int[] a) {
        array = a;
    }

    public int[] getSortedArray() {
        return sortedArray;
    }

    public int[] getOriginalArray() {
        return array;
    }

    public void sort() {
        sortedArray = array.clone();
        System.out.println(this);
        recursiveSort(sortedArray.length - 1); //Should subtract by length
    }

    public void recursiveSort(int endIndex) {
        System.out.println("Sorting with param : "+endIndex);
        if (endIndex >= 0) {
            int m = getMaxIndex(endIndex, sortedArray);
            swap(m, endIndex);
            System.out.println(this);
            recursiveSort(endIndex - 1);
        }
    }

    public int getMaxIndex(int endIndex, int[] a) {
        int max = a[0];
        int maxIndex = 0;
        for (int i = 1; i <= endIndex; i++) {
            if (a[i] > max) {
                max = a[i];
                maxIndex = i;
                System.out.println("Max index : " + i);
            }
        }
        return maxIndex;
    }

    public void swap(int src, int dest) {
        System.out.println("Swap : "+src+" to "+dest);
        int temp = sortedArray[dest];
        sortedArray[dest] = sortedArray[src];
        sortedArray[src] = temp;
    }

    public String toString() {
        return "Original: " + prettyPrint(getOriginalArray()) + "\n"
                + "Sorted:   " + prettyPrint(getSortedArray());
    }

    private String prettyPrint(int[] a) {
        String s = "";
        for (int i : a) {
            s += i + " ";
        }
        return s;
    }

    public static void main(String[] args) {
        // Automate running, but not testing
        int[] array = {5, 67, 12, 20};
        RecursiveSorter s = new RecursiveSorter(array);
        s.sort();
        //System.out.println(s); // uses Sorter.toString
    }
}
公共类递归分类器{
达雷私人酒店;
私有int[]数组;
公共递归分类器(){
数组=新整数[1];
}
公共递归分类器(int[]a){
数组=a;
}
公共void集合数组(int[]a){
数组=a;
}
public int[]getSorterDarray(){
返回至Darray;
}
公共int[]getOriginalArray(){
返回数组;
}
公共无效排序(){
sortedArray=array.clone();
System.out.println(本文件);
递归排序(sortedArray.length-1);//应减去长度
}
公共void递归排序(int-endIndex){
System.out.println(“使用参数排序:+endIndex”);
如果(endIndex>=0){
int m=getMaxIndex(endIndex,sortedDarray);
掉期(m,endIndex);
System.out.println(本文件);
递归排序(endIndex-1);
}
}
public int getMaxIndex(int endIndex,int[]a){
int max=a[0];
int maxIndex=0;
对于(int i=1;i max){
max=a[i];
maxIndex=i;
System.out.println(“最大索引:+i”);
}
}
返回最大索引;
}
公共无效掉期(内部src、内部目的地){
系统输出打印项次(“交换:+src+”到“+dest”);
int temp=分拣机[目的地];
SorterDarray[dest]=SorterDarray[src];
SorterDarray[src]=温度;
}
公共字符串toString(){
return“Original:+预打印(getOriginalArray())+”\n
+“排序:”+预打印(getSortedArray());
}
私有字符串预打印(int[]a){
字符串s=“”;
对于(int i:a){
s+=i+“”;
}
返回s;
}
公共静态void main(字符串[]args){
//自动化运行,但不自动化测试
int[]数组={5,67,12,20};
RecursiveSorter s=新的RecursiveSorter(阵列);
s、 排序();
//System.out.println(s);//使用Sorter.toString
}
}

“我认为有问题”-你想说得更具体一点吗?@Keppil是的,当我进行演练时,值与它们应该的值不匹配,这使我认为该方法中有一个错误,我似乎无法解决。这对吗?
sortedArray=array;
您正在创建对同一数组的第二个引用。@ABoschman注意到了,我的错误,谢谢。我应该抓到一些。