如何通过多级递归保留一个值?
How do I retain a value through multiple levels of recursion?
我编写了一个快速排序程序,它计算执行的交换次数以按升序对数组进行排序。在这个程序中,我使用了一个全局变量来计算交换次数,因为我不知道如何通过多级递归来保留值。我理解这样一个概念,即当函数折叠自身时,要通过多级递归来保留值,但我显然无法实现它。有人可以建议我这样做的方法吗?
import java.util.Scanner;
public class QuickSort {
// global variable for counting the quicksort shifts.
private static int swapCount = 0;
public static void main(String[] args) {
// scanning the values;
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int ar[] = new int[N];
for(int i = 0 ; i < N ; i++){
int value = scan.nextInt();
ar[i] = value;
}
quickSort(ar, 0, ar.length-1);
System.out.println(swapCount);
}
//quickSort
public static void quickSort(int ar[], int start, int end){
if(start<end){
int pIndex = partition(ar, start, end);
quickSort(ar,start, pIndex-1);
quickSort(ar, pIndex+1, end);
}
}
// partition function
public static int partition(int ar[], int start, int end){
int pivot = ar[end];
int pIndex = start;
for (int i = start ; i < end ; i++ ){
if(ar[i] < pivot){
int temp = ar[i];
ar[i] = ar[pIndex];
ar[pIndex] = temp;
swapCount++;
pIndex++;
}
}
int temp = ar[end];
ar[end] = ar[pIndex];
ar[pIndex] = temp;
swapCount++;
return pIndex;
}
}
您面临的问题是,在 Java 中,如果您在函数 returns。解决这个问题的方法,即使它不一定是 "good style",也是将 Class 对象而不是原始对象传递给函数,然后更改为 class object在函数内部创建的成员变量,后面会在外部反映出来。
// in main()
Integer nbrSwaps = new Interger(0);
quickSort(ar, 0, ar.length-1, nbrSwaps);
//quickSort
public static void quickSort(int ar[], int start, int end, Integer swapCount) {
if(start<end){
int pIndex = partition(ar, start, end, swapCount);
quickSort(ar,start, pIndex-1, swapCount);
quickSort(ar, pIndex+1, end, swapCount);
}
}
// partition function
public static int partition(int ar[], int start, int end, Integer swapCount) {
... as before ...
}
我编写了一个快速排序程序,它计算执行的交换次数以按升序对数组进行排序。在这个程序中,我使用了一个全局变量来计算交换次数,因为我不知道如何通过多级递归来保留值。我理解这样一个概念,即当函数折叠自身时,要通过多级递归来保留值,但我显然无法实现它。有人可以建议我这样做的方法吗?
import java.util.Scanner;
public class QuickSort {
// global variable for counting the quicksort shifts.
private static int swapCount = 0;
public static void main(String[] args) {
// scanning the values;
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int ar[] = new int[N];
for(int i = 0 ; i < N ; i++){
int value = scan.nextInt();
ar[i] = value;
}
quickSort(ar, 0, ar.length-1);
System.out.println(swapCount);
}
//quickSort
public static void quickSort(int ar[], int start, int end){
if(start<end){
int pIndex = partition(ar, start, end);
quickSort(ar,start, pIndex-1);
quickSort(ar, pIndex+1, end);
}
}
// partition function
public static int partition(int ar[], int start, int end){
int pivot = ar[end];
int pIndex = start;
for (int i = start ; i < end ; i++ ){
if(ar[i] < pivot){
int temp = ar[i];
ar[i] = ar[pIndex];
ar[pIndex] = temp;
swapCount++;
pIndex++;
}
}
int temp = ar[end];
ar[end] = ar[pIndex];
ar[pIndex] = temp;
swapCount++;
return pIndex;
}
}
您面临的问题是,在 Java 中,如果您在函数 returns。解决这个问题的方法,即使它不一定是 "good style",也是将 Class 对象而不是原始对象传递给函数,然后更改为 class object在函数内部创建的成员变量,后面会在外部反映出来。
// in main()
Integer nbrSwaps = new Interger(0);
quickSort(ar, 0, ar.length-1, nbrSwaps);
//quickSort
public static void quickSort(int ar[], int start, int end, Integer swapCount) {
if(start<end){
int pIndex = partition(ar, start, end, swapCount);
quickSort(ar,start, pIndex-1, swapCount);
quickSort(ar, pIndex+1, end, swapCount);
}
}
// partition function
public static int partition(int ar[], int start, int end, Integer swapCount) {
... as before ...
}