How does this function work?

Asked

Viewed 86 times

1

It’s part of an sorting algorithm, reading the code made it seem to me that the function never makes the call: sort(mid+1, high); gets into loop at first sort(low, mid); until you leave the if, and that return I also don’t understand what it’s for.

void sort(int low, int high) {
   int mid;

   if(low < high) {

      mid = (low + high) / 2;

      sort(low, mid);
      sort(mid+1, high);
      merging(low, mid, high);
   } else { 
      return;
   }   
}
  • 1

    Explain further your doubt

  • it would be good if you hit the indentation of the code to facilitate the understanding of the same...put 4 spaces at the beginning of each line

  • 2

    Here you can find the explanation of this sorting method: https://pt.wikipedia.org/wiki/Merge_sort or your question is about the functioning of recursive functions?

  • It is part of an sorting algorithm, reading the code seemed to me that the function never makes the call: Sort(mid+1, high); it loops in the first Sort(low, mid); until it leaves the if, and this Return also did not understand what it is for.

  • It is the merge Sort implementation algorithm.

  • @Vitor Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how to do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

Show 1 more comment

1 answer

1

You have a recursive function there. Is it necessary somewhere to leave the function right? And it needs to be conditional because as this function is called by itself it goes running over and over again item by item, at some point it should not keep calling itself and need to leave, so if low is not less than high the function performs nothing (note that this last call serves only to force the output, has no execution) and terminates by ending the repetition of new calls and returning to the previous call that will continue running the rest of the code, so you can still run other parts of the code more often or you can start going down the stack.

It seems that the doubt is only at this point because knowing that at some point the execution does not occur and there is a return without doing anything it is possible to notice that one time the flame has control of the execution and comes out of what will be this recursive loop that seems infinite (and it would be even if I didn’t have this return). When you leave the function for the first time sort() she will start the second, which at some point will end as well and then will execute the mergeing() and until it closes and goes back to who called, who possibly will be herself, until it descends the level until arriving at the first call of the function.

  • Thank you very much, it helped a lot!

  • @Vitor see in the [tour] the best way to say thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.