可视化递归

时间:2016-01-02 17:09:20

标签: java recursion mergesort

我试图在堆栈中暂停和弹出值来跟踪mergesort中递归'发生'的方式。我可以理解算法是如何工作的,我可以按照纸上的步骤进行操作,但很难想象推送和弹出值的内容和时间。我的mergesort代码,修复为5,4,3,2,1,0的简单数组是:

public class MergeSort {

private static int[] ar = new int[6];

    public static void main(String[] args) throws Exception {
        MergeSort s=new MergeSort();   
        ar[0]=5;ar[1]=4;ar[2]=3;ar[3]=2;ar[4]=1;ar[5]=0;
        s.mergesort(ar);
    }


        static int[] mergesort(int[] arr){
        printArray(arr);
        System.out.println(" ");

        int size=arr.length;

        if(size == 1){

            return arr;
        }

        else{

           int[] ar1=new int[size/2];
           int[] ar2=new int[size-ar1.length];
           System.arraycopy(arr, 0, ar1, 0, ar1.length);
           System.arraycopy(arr, ar1.length, ar2, 0, ar2.length);
           mergesort(ar1);
           mergesort(ar2);
           merge(ar1,ar2,arr);
           return arr;

        }

    }

    static int[] merge(int[] ar1,int[] ar2,int[] temp){


        int pointer1=0;
        int pointer2=0;
        int pointer3=0;
       while(pointer1 < ar1.length && pointer2 < ar2.length){
           if(ar1[pointer1] < ar2[pointer2]){
               temp[pointer3]=ar1[pointer1];

               pointer1++;
           }

           else{
               temp[pointer3] = ar2[pointer2];
               pointer2++;
           }
           pointer3++;
       }


        System.arraycopy(ar1, pointer1, temp, pointer3, ar1.length - pointer1);
        System.arraycopy(ar2, pointer2, temp, pointer3, ar2.length - pointer2);


      return temp;
    }


}

并生成输出:

5 4 3 2 1 0  
5 4 3  
5  
4 3  
4  
3  
2 1 0  
2  
1 0  
1  
0

正如预期的那样,函数将543推送到堆栈,然后推送5然后推送4和3,依此类推,然后是什么?如何以递归方式合并值?

2 个答案:

答案 0 :(得分:1)

有许多非常有用的网站可视化排序算法。这个将指导您完成排序和代码。

http://visualgo.net/sorting.html#

其他人:

[1] http://www.sorting-algorithms.com/常见排序,大O复杂性,可视化等的比较

[2] https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html良好的可视化,无需解释

[3] http://sorting.at/很漂亮,没有解释

答案 1 :(得分:1)

跟踪递归是一件很有用的事情。

其他一个答案指出你对其他人做过的一些可视化。

我认为您可以将自己的跟踪添加到您自己的代码中。考虑到这一点,请考虑以下代码:

我发现有用的两件事是添加深度计数器和标签,因此您可以在“上下文”中查看每个调用。 这是当前输出的样子 - 我讨论了向merge()添加跟踪信息,但决定这将是一个有用的插图。

诀窍是修改printArray()以缩进以适应当前深度。 添加一个只转储字符串的tracePrint()也是微不足道的;我起诉了printArray(),因为那是代码所使用的。

还要注意通过&gt; mergsort()&amp ;;标记方法的惯例。 &LT;表示输入&amp;离开。

注意输出更详细,对于我正在编写的代码我不会使用所有这些调用printArray(),但我希望你发现它很有趣并希望它可以帮助你解决将来的递归问题。快乐的编码: - )

示例输出

$ java MergeSort 
>mergesort len=6 [ 5, 4, 3, 2, 1, 0 ]
|   >mergesort:ar1 len=3 [ 5, 4, 3 ]
|   |   >mergesort:ar1:ar1 len=1 [ 5 ]
|   |   <mergesort:ar1:ar1 len=1 [ 5 ]
|   |   >mergesort:ar1:ar2 len=2 [ 4, 3 ]
|   |   |   >mergesort:ar1:ar2:ar1 len=1 [ 4 ]
|   |   |   <mergesort:ar1:ar2:ar1 len=1 [ 4 ]
|   |   |   >mergesort:ar1:ar2:ar2 len=1 [ 3 ]
|   |   |   <mergesort:ar1:ar2:ar2 len=1 [ 3 ]
|   |   >merge:ar1      len=1 [ 4 ]
|   |    merge:ar2      len=1 [ 3 ]
|   |    merge:temp     len=2 [ 4, 3 ]
|   |    temp.afterWhile len=2 [ 3, 3 ]
|   |    temp.acopyAR1  len=2 [ 3, 4 ]
|   |   <merge.acopyAR2  len=2 [ 3, 4 ]
|   |   <mergesort:ar1:ar2 len=2 [ 3, 4 ]
|   >merge:ar1      len=1 [ 5 ]
|    merge:ar2      len=2 [ 3, 4 ]
|    merge:temp     len=3 [ 5, 4, 3 ]
|    temp.afterWhile len=3 [ 3, 4, 3 ]
|    temp.acopyAR1  len=3 [ 3, 4, 5 ]
|   <merge.acopyAR2  len=3 [ 3, 4, 5 ]
|   <mergesort:ar1 len=3 [ 3, 4, 5 ]
|   >mergesort:ar2 len=3 [ 2, 1, 0 ]
|   |   >mergesort:ar2:ar1 len=1 [ 2 ]
|   |   <mergesort:ar2:ar1 len=1 [ 2 ]
|   |   >mergesort:ar2:ar2 len=2 [ 1, 0 ]
|   |   |   >mergesort:ar2:ar2:ar1 len=1 [ 1 ]
|   |   |   <mergesort:ar2:ar2:ar1 len=1 [ 1 ]
|   |   |   >mergesort:ar2:ar2:ar2 len=1 [ 0 ]
|   |   |   <mergesort:ar2:ar2:ar2 len=1 [ 0 ]
|   |   >merge:ar1      len=1 [ 1 ]
|   |    merge:ar2      len=1 [ 0 ]
|   |    merge:temp     len=2 [ 1, 0 ]
|   |    temp.afterWhile len=2 [ 0, 0 ]
|   |    temp.acopyAR1  len=2 [ 0, 1 ]
|   |   <merge.acopyAR2  len=2 [ 0, 1 ]
|   |   <mergesort:ar2:ar2 len=2 [ 0, 1 ]
|   >merge:ar1      len=1 [ 2 ]
|    merge:ar2      len=2 [ 0, 1 ]
|    merge:temp     len=3 [ 2, 1, 0 ]
|    temp.afterWhile len=3 [ 0, 1, 0 ]
|    temp.acopyAR1  len=3 [ 0, 1, 2 ]
|   <merge.acopyAR2  len=3 [ 0, 1, 2 ]
|   <mergesort:ar2 len=3 [ 0, 1, 2 ]
>merge:ar1      len=3 [ 3, 4, 5 ]
 merge:ar2      len=3 [ 0, 1, 2 ]
 merge:temp     len=6 [ 5, 4, 3, 2, 1, 0 ]
 temp.afterWhile len=6 [ 0, 1, 2, 2, 1, 0 ]
 temp.acopyAR1  len=6 [ 0, 1, 2, 3, 4, 5 ]
<merge.acopyAR2  len=6 [ 0, 1, 2, 3, 4, 5 ]
<mergesort len=6 [ 0, 1, 2, 3, 4, 5 ]
$ 

MergeSort.java

public class MergeSort {

    private static int[] ar = new int[6];

    public static void main(String[] args) throws Exception {
        ar[0]=5;ar[1]=4;ar[2]=3;ar[3]=2;ar[4]=1;ar[5]=0;
        // note: could also do this:
        // ar = = new int[6] { 5, 4, 3, 2, 1, 0 };


        MergeSort s=new MergeSort();
        s.mergesort(0, "", ar);

        // fwiw... all of the methods and members (well, just 'ar')
        // are static so the following is equivalent to the above.
        //    MergeSort.mergesort( 0, "", ar );
    }

    // added: depth & label to provide context for recursion.
    static int[] mergesort(int depth, String label, int[] arr){
        printArray(depth, ">mergesort"+label, arr);
        //System.out.println(" ");

        int size=arr.length;

        if(size >= 2) {
           int[] ar1=new int[size/2];
           int[] ar2=new int[size-ar1.length];
           System.arraycopy(arr, 0, ar1, 0, ar1.length);
           System.arraycopy(arr, ar1.length, ar2, 0, ar2.length);
           // note that we're appending labels so all subsequent
           // calls to mergesort() will have their 'lineage' baked
           // into the label.
           mergesort( 1+depth, label+":ar1", ar1);
           mergesort( 1+depth, label+":ar2", ar2);
           merge(depth, ar1, ar2, arr);
        }
        printArray(depth, "<mergesort"+label, arr);
        return arr; // single return point.
    }

    // added: depth & label aren't so useful here, but just for fun.
    static int[] merge(int depth, int[] ar1,int[] ar2,int[] temp){
       // note: tweaked spacing here to make these line up "nicely"
       // with the longer labels below, like "temp.afterWHile".
       // (output looks better, here just looks messy).
       printArray( depth, ">merge:ar1     ", ar1 );
       printArray( depth, " merge:ar2     ", ar2 );
       printArray( depth, " merge:temp    ", temp );

       int pointer1=0;
       int pointer2=0;
       int pointer3=0;
       while(pointer1 < ar1.length && pointer2 < ar2.length){
          if(ar1[pointer1] < ar2[pointer2]){
             temp[pointer3]=ar1[pointer1];
             pointer1++;
          }
          else{
             temp[pointer3] = ar2[pointer2];
             pointer2++;
          }
          pointer3++;
       }
       printArray( depth, " temp.afterWhile", temp );
       System.arraycopy(ar1, pointer1, temp, pointer3, ar1.length - pointer1);
       printArray( depth, " temp.acopyAR1 ", temp );
       System.arraycopy(ar2, pointer2, temp, pointer3, ar2.length - pointer2);
       printArray( depth, "<merge.acopyAR2 ", temp );
       return temp;
    }

    // wrote a simple printArray() that uses depth & label.
    static void printArray( int depth, String label, int[] array ) {
       for( int i = 0; i < depth; ++i ) {
          System.out.print("|   "); // indent to depth.
       }
       System.out.print( label );
       System.out.print( " len="+array.length+" [ " );
       String delim = "";
       for( int i = 0; i < array.length; ++i ) {
          System.out.print( delim + array[i] );
          delim = ", ";
       }
       System.out.println( " ]"); // end the line we just created.
    }
}