def quicksort(A):
    N = len(A)
    if N <= 1:
        return A

    pivot = A[N - 1]
    left = []
    right = []

    for i in range(N - 1):
        if A[i] <= pivot:
            left.append(A[i])
        else:
            right.append(A[i])

    return quicksort(left) + [pivot] + quicksort(right)

print(quicksort([6, 3, 8, 1, 5, 2, 7, 4]))
