Two recursive calls in a Merge Sort function confusion

前端 未结 9 1546
情书的邮戳
情书的邮戳 2021-02-19 13:37

I have been out of touch with Algorithms for a while and have start revising my concepts these days. To my surprise the last i remember of my recursions skill was that i was goo

9条回答
  •  心在旅途
    2021-02-19 13:42

    Just follow the execution...

    First call 0,7 --> enters if, middle = 3 (integer division), calls again as (0,3)
    Second call 0,3 --> enters if, middle = 1, calls again as (0,1)
    Third call 0,1 --> enters if, middle = 0, calls again as (0,0)
    Fourth call 0,0 --> does not enter if, back to third call
    Third call 0,1 --> calls as middle+1,high which is (1,1)
    Fifth call 1,1 --> does not enter if, back to third call
    Third call 0,1 --> calls the string you didn't expect
    

    can continue on but that is where the string you aren't expecting is executed.

提交回复
热议问题