Hướng dẫn quicksort 3-way partition python

I'm a new to algorithms and I'm confused as to where are the errors in my code that I'm writing as an assignment. I'm trying to implement a quicksort algorithm in Python 3 that deals with equal values in the array.

Here's a quicksort function (a stands for the array):

def randomized_quick_sort(a, l, r):
    if l >= r:
        return
    k = random.randint(l, r)
    a[l], a[k] = a[k], a[l]
    m1, m2 = partition3(a, l, r)
    randomized_quick_sort(a, l, m1 - 1);
    randomized_quick_sort(a, m2 + 1, r);

And here's my partition function:

def partition3(a, l, r):
    x, j, t = a[l], l, r
    for i in range(l + 1, r + 1):
        if a[i] < x:
            j +=1
            a[i], a[j] = a[j], a[i]
        elif a[i] > x:
            a[i], a[t] = a[t], a[i]
            t -=1
        else:
            j +=1
    a[l], a[j] = a[j], a[l]
    return j, t

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

def partition3(A, l, r):
"""
partition3: A partition for quicksort algorithm. We'll use the 3-way to handle few equal elements in array (happens
a lot in practical use.)
This function is called from the main function quick_sort.
"""
lt = l # We initiate lt to be the part that is less than the pivot
i = l # We scan the array from left to right
gt = r # The part that is greater than the pivot
pivot = A[l] # The pivot, chosen to be the first element of the array, that why we'll randomize the first elements position
# in the quick_sort function.
while i <= gt: # Starting from the first element.
if A[i] < pivot:
A[lt], A[i] = A[i], A[lt]
lt += 1
i += 1
elif A[i] > pivot:
A[i], A[gt] = A[gt], A[i]
gt -= 1
else:
i += 1
return lt, gt
def quick_sort(A, l, r):
"""
quick_sort: One of the most used sorting algorithm.
It makes to recursive calls. One to sort the left part separately, other for sorting the right part.
The partition key is chosen randomly via ``random.randint(l, r)`` and it's between the ``l, r``.
PARAMETERS:
-----------
A: Array or the sequence that we want to sort.
l: The lower bound of the array that we want to sort. It's not very important we might replace it by a wrapper function
that only takes in an array as input. In this case it's the first element in the left part of the array.
r: It's the same as l, only differs as it's the first element from the end.
RETURNS:
-------
Sorted list A.
"""
if l >= r:
return
k = random.randint(l, r)
A[k], A[l] = A[l], A[k]
lt, gt = partition3(A, l, r)
quick_sort(A, l, lt - 1)
quick_sort(A, gt + 1, r)