0

I have recently been refreshing my knowledge about the quick-sort algorithm. I am aware that a list (Python) is passed by reference so that any changes made to its entries are reflected outside the function call. The algorithm uses recursion without a return statement. I want to know what happens after the function reaches the end of its body. The return statement, if present, passes a value back to the immediate caller before "popping" off the stack (correct me if i am wrong). This continues to happen until the last caller is reached. If there is no return call, then does the aforementioned process happen?

def quick_sort(arr,low,high):
    # the Breaking statement
    if (low < high):
       # Partitoning
        div = partition(arr,low,high)
        quick_sort(arr,low,div) 
        quick_sort(arr,div+1,high)

def partition(arr,low,high):
    pivot = arr[low]
    minIndex = low;
    for i in range(low+1,high):
        if arr[i] <= pivot:
            minIndex += 1
            arr[minIndex],arr[i] = arr[i],arr[minIndex]
    arr[minIndex],arr[low] = pivot,arr[minIndex];
    return  minIndex
Moaz _
  • 15
  • 4

1 Answers1

1

When a python function reach its end, it returns None.

https://www.askpython.com/python/python-return-statement

So the last call of quick_sort returns None, then the previous one returns None as it reach its own end, and so on until the first call.

But as you don't use the return value of quick_sort (at least in the code you provide), the return value does not matter.

Valentin M.
  • 520
  • 5
  • 19
  • i am aware of that, but does the whole stack removal process occur like with the return call? If so, then does that mean that the none return value acts as the signal to start the removal process from the stack – Moaz _ Jan 15 '20 at 15:34
  • The removal from the stack happens at the end of a code block regardless of the return value. As you do not use the return value, it changes nothing. – Valentin M. Jan 15 '20 at 15:43