Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
递归循环在Java中再次运行而不调用它_Java_Sorting_Recursion - Fatal编程技术网

递归循环在Java中再次运行而不调用它

递归循环在Java中再次运行而不调用它,java,sorting,recursion,Java,Sorting,Recursion,有人知道为什么这个排序递归的例子在第二次调用递归方法后仍然运行吗。在第二次调用后,由于某种原因,当i==high_index两者都位于2时,我在internet上找到的这段代码再次运行同一行,但带有一些奇怪的值i=4和high_index=6。我就是看不到虫子。所有的递归都应该在第二次之后停止,并且数组应该被排序 请参阅带有注释的行//为什么这行在一行中以不同的值运行两次???? package dataClass; import java.util.Arrays; public class

有人知道为什么这个排序递归的例子在第二次调用递归方法后仍然运行吗。在第二次调用后,由于某种原因,当
i==high_index
两者都位于
2
时,我在internet上找到的这段代码再次运行同一行,但带有一些奇怪的值
i=4
high_index=6
。我就是看不到虫子。所有的递归都应该在第二次之后停止,并且数组应该被排序

请参阅带有注释的行<代码>//为什么这行在一行中以不同的值运行两次????

package dataClass;

import java.util.Arrays;
public class QuickSort {

    private int temp_array[];
    private int len;

    public void sort(int[] nums) {

        if (nums == null || nums.length == 0) {
            return;
        }
        this.temp_array = nums;
        len = nums.length;
        quickSort(0, len - 1, "First");
        //System.out.println("what");
    }
     private void quickSort(int low_index, int high_index, String one_two) {
        System.out.println("***" + low_index + " " + high_index);
        System.out.println(one_two);
        int i = low_index;
        int j = high_index;
        // calculate pivot number 
        int pivot = temp_array[low_index+(high_index-low_index)/2];
        // Divide into two arrays
        System.out.println(Arrays.toString(temp_array));
        while (i <= j) {
               while (temp_array[i] < pivot) {
                   System.out.println("This happens");
                i++;
            }
            while (temp_array[j] > pivot) {
                System.out.println("Or this happens");
                j--;
            }
            if (i <= j) {
                System.out.println("Execute");

                exchangeNumbers(i, j);
                //move index to next position on both sides
                i++;
                j--;
                System.out.println("i=" + i + " high_index=" + high_index);
            }
        }
        // call quickSort() method recursively

        if (low_index < j) {
            System.out.println("Running 1");
            System.out.println(j + " " + low_index);
            quickSort(low_index, j, "Run 1---------");            
        }


        System.out.println("**i=" + i + " **high_index=" + high_index);  // WHY DOES THIS LINE RUN TWICE IN A ROW WITH DIFFERENT VALUES?????
        System.out.println("Why run again without call to quickSort()?");
        if (i < high_index) {
            System.out.println("Running 2");
            System.out.println(i + " " + high_index);
            quickSort(i, high_index, "Run 2---------");
        }


    }

    private void exchangeNumbers(int i, int j) {
        int temp = temp_array[i];
        temp_array[i] = temp_array[j];
        temp_array[j] = temp;
    }

     // Method to test above
    public static void main(String args[])
    {
        QuickSort ob = new QuickSort();
        int nums[] = {7, -5, 3, 2, 1, 0, 45};
        System.out.println("Original Array:");
        System.out.println(Arrays.toString(nums));
        ob.sort(nums);
        System.out.println("Sorted Array");
        System.out.println(Arrays.toString(nums));
    }
}
包数据类;
导入java.util.array;
公共类快速排序{
私有int temp_数组[];
私家侦探;
公共无效排序(int[]nums){
if(nums==null | | nums.length==0){
返回;
}
this.temp_数组=nums;
len=nums.长度;
快速排序(0,len-1,“第一”);
//System.out.println(“什么”);
}
私有void快速排序(int-low\u索引、int-high\u索引、字符串1\u-two){
System.out.println(“***”+低索引+”+高索引);
系统输出打印LN(1×2);
int i=低指数;
int j=高指数;
//计算轴数
int pivot=temp_数组[low_index+(high_index-low_index)/2];
//分成两个数组
System.out.println(Arrays.toString(temp_数组));
而(我){
System.out.println(“或发生这种情况”);
j--;
}

如果(i虽然我没有在这里真正跟踪完整的逻辑,但我相信“return”语句丢失了。当control raache递归方法调用时,该方法将再次执行。但是,一旦递归调用的执行完成,原始方法调用的执行将恢复,并且您将看到意外的行为

返回(中断执行流)在进行递归调用时,也在其他位置返回,以防止进一步执行预期的代码块

if (low_index < j) {
            System.out.println("Running 1");
            System.out.println(j + " " + low_index);
            quickSort(low_index, j, "Run 1---------");
            //recursive code invoked, but prevent the control to process downstream code
            return;
        }
if(低指数
使用调试器逐步完成,所有内容都会显示出来。提示:假设在第一个条件块中进行了递归调用,而在该调用中没有调用第二个条件块?1)递归调用中打印的最后一个内容是什么?2)从递归调用返回后立即发生什么?啊,现在给出了有意义的答案,它运行对initial方法的第一个调用并继续,实际上它使用这些调用中的最后一个值i,j,low和high inde完成了所有以前的调用x、 谢谢