因此代码的基本整体功能是接收数组并按升序排序。我的代码中没有错误,但我认为有些错误。我做了一些测试用例,有些已经失败了,我感觉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
}
}
答案 0 :(得分:0)
交换方法有错误:
a[dest] = src;
应该是:
a[dest] = a[src];
此行也不会复制数组,因此array
和sortedArray
引用相同的数组对象。
sortedArray = array;
将其替换为:
sortedArray = Arrays.copyOf(array, array.length);
同样在方法getMaxIndex
中缺少等号:
for (int i = 1; i < endIndex; i++) {
应该是:
for (int i = 1; i <= endIndex; i++) {
答案 1 :(得分:0)
1 - 您必须将数组克隆(或复制)到sortedArray。
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
}
}