Recursion AWK失败的递归调用?

Recursion AWK失败的递归调用?,recursion,scope,awk,quicksort,Recursion,Scope,Awk,Quicksort,我正在尝试实现中定义的快速排序的简单变体。然而,在我看来,旧的递归调用中的局部变量正在泄漏到后面的调用中。(我目前的解释)。是这样吗?有解决办法吗 下面是示例代码 # Quick sort - Simple variant. Requires 0(n) extra store # Divide and conquer. Pick a pivot, compare elements to # pivot generating two sublists; one greater than pivo

我正在尝试实现中定义的快速排序的简单变体。然而,在我看来,旧的递归调用中的局部变量正在泄漏到后面的调用中。(我目前的解释)。是这样吗?有解决办法吗

下面是示例代码

# Quick sort - Simple variant. Requires 0(n) extra store 
# Divide and conquer. Pick a pivot, compare elements to
# pivot generating two sublists; one greater than pivot 
# and the other less than pivot. Sort these two recursively.
# See http://en.wikipedia.org/wiki/Quicksort#Simple_version

function dump_array(arr_arg){

        arr_arg_len = length(arr_arg)
        RSEP = ORS;
        ORS=" ";
        dctr = 1; 
        # Do not use length(arr_arg) in place of arr_arg
        # It fails. The following doesn't work
        #while (dctr <= arr_arg_len){
        while (dctr <= arr_arg_len){
                print arr_arg[dctr]; 
                dctr++;
        }; 
        ORS = RSEP;
        print "\n";
}


function simple_quicksort(unsorted_str)
{

        # Unpack from the str - space separated
        print "******************************"
        print "Called with "unsorted_str
        # Split the space separated string into an array
        split(unsorted_str, unsorted_array, " ");

        array_len = length(unsorted_array);

        # No more sorting to be done. Break recursion
        if (array_len <= 1){
                print "Ending recursion with "unsorted_str
                return unsorted_str
        }
        # Pick a random value as pivot
        # index must not be 0
        idx = 0
        while (idx == 0){
                srand()
                idx = int(rand() * array_len)
        }
        pivot = unsorted_array[idx]
        if (debug >= 1){
                print "idx:"idx" pivot is: "pivot
        }

        num = 1;
        # we don't use the zero'th element,
        # this helps us declare an empty array
        # dunno any other method
        # we'll remove it anyway
        less_arr[0] = 0
        less_ctr = 1
        more_arr[0] = 0
        more_ctr = 1
        while (num <= array_len){ 
                # Skip pivot
                if (idx != num){
                        if (unsorted_array[num] <= pivot){
                                if (debug >= 1){
                                        print "Element less than pivot: "unsorted_array[num]
                                }
                                less_arr[less_ctr] = unsorted_array[num]
                                less_ctr++;
                        }else{
                                if (debug >= 1){
                                        print "Element more than pivot: "unsorted_array[num]
                                }
                                more_arr[more_ctr] = unsorted_array[num]
                                more_ctr++;
                        }
                }
                num++
        };
        # strip out the holder in idx 0
        delete less_arr[0]
        delete more_arr[0]
        if (debug >= 1){
                print "Less than pivot:"
                print dump_array(less_arr)
                print "More than pivot:"
                print dump_array(more_arr)
        }

        # Marshal array back to a string
        less_str=""
        less_length = length(less_arr)
        num = 1
        print "Less length: "less_length
        while (num <= less_length){
                less_str = less_str" "less_arr[num]     
                num++;
        }

        # same thing for more
        more_str=""
        more_length = length(more_arr) 
        num = 1
        while (num <= more_length){
                more_str = more_str" "more_arr[num]
                num++;
        }

        if (debug >= 1){
                print "Going for a recursive call with elements < pivot: "less_str
                print "Going for a recursive call with elements > pivot: "more_str
                print "pivot was: "pivot
        }

        # Tried to delete the local variables
        # Coz it seems like local vars are visible to recursed functions
        # Is this why it fails?
        delete less_arr
        delete more_arr
        delete unsorted_array
        print "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
        print ""

        return simple_quicksort(less_str) " "pivot" "simple_quicksort(more_str)


}

BEGIN{
        print "-- quick sort --"
}

{
        # print the unsorted objects
        print "Unsorted "NF" objects:\n"$0; 

        # We'll use a slightly different method,
        # Pass the $0 string to the sorter, let it split
        # it into an array, qsort that array, generate sub- 
        # strings and recursively qsort them

        #Simple version
        sorted = simple_quicksort($0)

}

END{
        print "Sorted "NF" objects"; 
        print "Sorted >>"sorted
}
#快速排序-简单变体。需要0(n)个额外存储
#分而治之。选择一个轴,将图元与
#枢轴生成两个子列表;比枢轴大一个
#另一个比枢轴小。对这两个进行递归排序。
#看http://en.wikipedia.org/wiki/Quicksort#Simple_version
函数转储数组(arr\u arg){
arr_arg_len=长度(arr_arg)
RSEP=ORS;
ORS=“”;
dctr=1;
#不要用长度(arr_arg)代替arr_arg
#它失败了。以下操作不起作用
#而(dctr枢轴:12 7 13719 28019 21444 30578 30647
数据为:5
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
******************************
电话号码是2
以2结束递归
******************************
致电12 7 13719 28019 21444 30578 30647
idx:1轴是:12
元素小于枢轴:7
元素多于枢轴:13719
元素多于枢轴:28019
元素多于枢轴:21444
元素多于枢轴:30578
元素多于枢轴:30647
小于枢轴:
7.
不仅仅是支点:
13719 28019 21444 30578 30647 
小于长度:1
使用元素pivot进行递归调用:13719 28019 21444 30578 30647
数据为:12
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
******************************
7点打电话
以7结束递归
******************************
致电13719 28019 21444 30578 30647
idx:3枢轴为:21444
元素小于枢轴:13719
元素多于枢轴:28019
元素多于枢轴:30578
元素多于枢轴:30647
小于枢轴:
13719
不仅仅是支点:
28019 30578 30647 
小于长度:1
使用元素进行递归调用pivot进行递归调用:28019 30578 30647
数据为:21444
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
******************************
电话是13719
以13719结束递归
******************************
致电28019 30578 30647
idx:1枢轴为:28019
元素多于枢轴:30578
元素多于枢轴:30647
小于枢轴:
不仅仅是支点:
30578 30647 
小于长度:0
使用元素pivot进行递归调用:30578 30647
数据为:28019
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
******************************
打电话给
以结束递归
******************************
致电3057830647
idx:1枢轴为:30578
元素多于枢轴:30647
小于枢轴:
不仅仅是支点:
30647
小于长度:0
使用元素pivot:30647进行递归调用
数据来源:30578
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
******************************
打电话给
以结束递归
******************************
电话是30647
以30647结束递归
已排序的9个对象
已排序>>2 5 7 12 13719 21444 28019 30578 30647
那次跑步看起来不错。请与下一次跑步进行比较:

$ echo 5 12 7 2 13719 28019 21444 30578 30647 | awk -f devel/andorian-blog/awk/sorting/quick_sort.awk -v debug=1  
-- quick sort --
Unsorted 9 objects:
5 12 7 2 13719 28019 21444 30578 30647
******************************
Called with 5 12 7 2 13719 28019 21444 30578 30647
idx:6 pivot is: 28019
Element less than pivot: 5
Element less than pivot: 12
Element less than pivot: 7
Element less than pivot: 2
Element less than pivot: 13719
Element less than pivot: 21444
Element more than pivot: 30578
Element more than pivot: 30647
Less than pivot:
5 12 7 2 13719 21444 


More than pivot:
30578 30647 


Less length: 6
Going for a recursive call with elements < pivot:  5 12 7 2 13719 21444
Going for a recursive call with elements > pivot:  30578 30647
pivot was: 28019
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

******************************
Called with  5 12 7 2 13719 21444
idx:4 pivot is: 2
Element more than pivot: 5
Element more than pivot: 12
Element more than pivot: 7
Element more than pivot: 13719
Element more than pivot: 21444
Less than pivot:



More than pivot:
5 12 7 13719 21444 


Less length: 0
Going for a recursive call with elements < pivot: 
Going for a recursive call with elements > pivot:  5 12 7 13719 21444
pivot was: 2
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

******************************
Called with 
Ending recursion with 
******************************
Called with  5 12 7 13719 21444
idx:3 pivot is: 7
Element less than pivot: 5
Element more than pivot: 12
Element more than pivot: 13719
E    lement more than pivot: 21444
Less than pivot:
5 


More than pivot:
12 13719 21444 


Less length: 1
Going for a recursive call with elements < pivot:  5
Going for a recursive call with elements > pivot:  12 13719 21444
pivot was: 7
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

******************************
Called with  5
Ending recursion with  5
******************************
Called with  12 13719 21444
idx:2 pivot is: 13719
Element less than pivot: 12
Element more than pivot: 21444
Less than pivot:
1    2 


More than pivot:
21444 


Less length: 1
Going for a recursive call with elements < pivot:  12
Going for a recursive call with elements > pivot:  21444
pivot was: 13719
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

******************************
Called with  12
Ending recursion with  12
******************************
Called with  21444
Ending recursion with  21444
******************************
Called with  21444
Ending recursion with  21444
Sorted 9 objects
Sorted >> 2  5 7  12 13719  21444 13719  21444
$echo 5 12 7 2 13719 28019 21444 30578 30647 | awk-f devel/androian blog/awk/sorting/quick_sort.awk-v debug=1
--快速排序--
未排序的9个对象:
5 12 7 2 13719 28019 21444 30578 30647
******************************
致电512721371928019214443057830647
idx:6枢轴is:28019
元素小于枢轴:5
小于枢轴的元素:12
元素小于枢轴:7
元素小于枢轴:2
元素小于枢轴:13719
元素小于枢轴:21444
元素多于枢轴:30578
元素多于枢轴:30647
小于枢轴:
5 12 7 2 13719 21444 
不仅仅是支点:
30578 30647 
小于长度:6
使用元素进行递归调用pivot进行递归调用:30578 30647
数据为:28019
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
******************************
致电512721371921444
idx:4枢轴为:2
元素多于枢轴:5
元素多于枢轴:12
元素多于枢轴:7
元素多于枢轴:13719
元素多于枢轴:21444
小于枢轴:
不仅仅是支点:
5 12 7 13719 21444 
小于长度:0
使用元素pivot进行递归调用:5 12 7 13719 21444
数据为:2
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
******************************
打电话给
以结束递归
******************************
致电51271371921444
idx:3轴是:7
元素小于枢轴:5
元素多于枢轴:12
元素多于枢轴:13719
E超过枢轴:21444
小于枢轴:
5.
不仅仅是支点:
12 13719 21444 
小于长度:1
使用元素pivot进行递归调用:12 13719 21444
数据为:7
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
******************************
打电话给5
以5结束递归
******************************
电话:121371921444
idx:2枢轴为:13719
小于枢轴的元素:12
元素多于枢轴:21444
小于枢轴:
1    2 
不仅仅是支点:
21444
小于长度:1
使用元素pivot:21444进行递归调用
数据来源:13719
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
******************************
12人打电话
以12结束递归
******************************
电话是21444
以21444结束递归
******************************
电话是21444
以21444结束递归
已排序的9个对象
已排序>>2 5 7 12 13719 21444 13719 21444

在AWK中,函数中引用的所有变量都是全局变量。没有“局部变量”之类的东西。
$ echo 5 12 7 2 13719 28019 21444 30578 30647 | awk -f devel/andorian-blog/awk/sorting/quick_sort.awk -v debug=1  
-- quick sort --
Unsorted 9 objects:
5 12 7 2 13719 28019 21444 30578 30647
******************************
Called with 5 12 7 2 13719 28019 21444 30578 30647
idx:6 pivot is: 28019
Element less than pivot: 5
Element less than pivot: 12
Element less than pivot: 7
Element less than pivot: 2
Element less than pivot: 13719
Element less than pivot: 21444
Element more than pivot: 30578
Element more than pivot: 30647
Less than pivot:
5 12 7 2 13719 21444 


More than pivot:
30578 30647 


Less length: 6
Going for a recursive call with elements < pivot:  5 12 7 2 13719 21444
Going for a recursive call with elements > pivot:  30578 30647
pivot was: 28019
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

******************************
Called with  5 12 7 2 13719 21444
idx:4 pivot is: 2
Element more than pivot: 5
Element more than pivot: 12
Element more than pivot: 7
Element more than pivot: 13719
Element more than pivot: 21444
Less than pivot:



More than pivot:
5 12 7 13719 21444 


Less length: 0
Going for a recursive call with elements < pivot: 
Going for a recursive call with elements > pivot:  5 12 7 13719 21444
pivot was: 2
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

******************************
Called with 
Ending recursion with 
******************************
Called with  5 12 7 13719 21444
idx:3 pivot is: 7
Element less than pivot: 5
Element more than pivot: 12
Element more than pivot: 13719
E    lement more than pivot: 21444
Less than pivot:
5 


More than pivot:
12 13719 21444 


Less length: 1
Going for a recursive call with elements < pivot:  5
Going for a recursive call with elements > pivot:  12 13719 21444
pivot was: 7
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

******************************
Called with  5
Ending recursion with  5
******************************
Called with  12 13719 21444
idx:2 pivot is: 13719
Element less than pivot: 12
Element more than pivot: 21444
Less than pivot:
1    2 


More than pivot:
21444 


Less length: 1
Going for a recursive call with elements < pivot:  12
Going for a recursive call with elements > pivot:  21444
pivot was: 13719
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

******************************
Called with  12
Ending recursion with  12
******************************
Called with  21444
Ending recursion with  21444
******************************
Called with  21444
Ending recursion with  21444
Sorted 9 objects
Sorted >> 2  5 7  12 13719  21444 13719  21444