Home>

I want to display the contents of Quicksort

Error message

I want to sort and print the contents of a quick sort made in Python, but it doesn't work. I want to help.
Error message

Python3.7
Source code
A = list [9, 2, 7, 5, 4,100,50,32,33]
def quick_sort (A):
n = len (A)
if n<= 1:
return A
pivot = A [0]
right = []
left = []
for i in range (1, n):
if A [i]<= pivot:
left.append (A [i])
else:
right.append (A [i])

R = quick_sort (right)
L = quick_sort (left)

return L + pivot + R

print (quick_sort (A))
Nothing was displayed.

  • Answer # 1

    # Change 1
    #A = list [9, 2, 7, 5, 4,100,50,32,33]
    A = [9, 2, 7, 5, 4,100,50,32,33]
    def quick_sort (A):
        n = len (A)
        if n<= 1:
            return A
        pivot = A [0]
        right = []
        left = []
        for i in range (1, n):
            if A [i]<= pivot:
                left.append (A [i])
            else:
                right.append (A [i])
        R = quick_sort (right)
        L = quick_sort (left)
        #Change 2
        #return L + pivot + R
        return L + [pivot] + R
    print (quick_sort (A)) # =>[2, 4, 5, 7, 9, 32, 33, 50, 100]


    The changes are as commented.
    I think that an error message was displayed when you executed the original code, so check the contents of the error message against the changes.

Related articles