[Python] quick sort python recursion

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

  1. Stack

    Stack Membro Participativo

    This is my quick sort code, the partition function works well, but I got a problem while calling the recursion. The pos changes every time it starts the function and then the list limits are change as well. How to fix that?

    def partition(lst, start, end):

    pos=0
    if len(lst)<2:
    return
    for i in range(len(lst[start:end])):
    if lst < lst[end]:
    lst,lst[pos]=lst[pos],lst
    pos+=1

    elif i==(len(lst[start:end])-1):
    lst[end],lst[pos]=lst[pos],lst[end]

    return pos

    def quick_sort_recursive(lst, start, end):

    pos=partition(lst, start, end)
    if start<=pos<=end :
    quick_sort_recursive(lst, start, pos-1)
    quick_sort_recursive(lst, pos+1, end)
    else:
    return lst

    Continue reading...

Compartilhe esta Página