[Python] Python Quick Sort implementation misses out duplicate elements

Discussão em 'Python' iniciado por Stack, Fevereiro 24, 2025.

  1. Stack

    Stack Membro Participativo

    I am implementing Quick Sort in Python. My code succeeds in sorting the list, but fails to include the duplicate elements. Please help me find the bug.

    from random import choice

    def quickSort(lst):
    if not lst:
    return []
    else:
    pivot = choice(lst)
    lesser = quickSort([l for l in lst if l < pivot])
    greater = quickSort([l for l in lst if l > pivot])
    #print lesser, pivot, greater
    return lesser + [pivot] + greater

    print quickSort([3, 2, 5, 6, 1, 7, 2, 4,234, 234, 23, 1234, 24, 132])


    Output:

    [1, 2, 3, 4, 5, 6, 7, 23, 24, 132, 234, 1234]

    Continue reading...

Compartilhe esta Página