Python counting changes and compares in Quick-sort function

Viewed 47

I've made a well-known Quick-Sort function and I want to implement counter of changes and compares, but I can't figure it how. I know how to pass variable within two function, but I have no idea how it works when I do it in recursive function.

def Partition(array, low, high):
    pivot = array[high]
    i = low - 1
    for j in range(low, high):
        if array[j] <= pivot:
            i += 1
            array[i], array[j] = array[j], array[i]
    array[i + 1], array[high] = array[high], array[i + 1]
    return i + 1

def QuickSort(array, low, high):
    if low < high:
        pivot = Partition(array, low, high)
        QuickSort(array, low, pivot - 1)
        QuickSort(array, pivot + 1, high)
    return array

print(QuickSort(array, 0, len(array) - 1))

Here is an example how I was passing the variable within two different function

def test2(changes, compares):
    changes += 1
    compares += 1
    return changes, compares

def test1(changes = 0, compares = 0):
    changes, compares = test2(changes, compares)
    return changes, compares
0 Answers
Related