[Python] Is there a logic error in my custom Quick Sort implementation in Python?

Discussão em 'Python' iniciado por Stack, Maio 17, 2025.

  1. Stack

    Stack Membro Participativo

    Is there a logic error in my custom Quick Sort implementation in Python?

    I'm trying to implement Quick Sort in Python without copying from tutorials. I wrote this version myself. It seems to work, but I want to know:

    • Is the logic correct?
    • Are there edge cases where it might fail?
    • Is there anything inefficient or wrong in the way I'm partitioning the array? Here's my code:

    my_array = [64, 34, 25, 12, 22, 11, 90, 5]

    def partition(array, low=0, high=None):

    pivot = array[high]
    i = low
    j = high - 1

    while i <= j :
    while array < pivot and i <= high:
    i+=1

    while array[j] >= pivot and j >= low:
    j-=1

    if i < j:
    array,array[j] = array[j], array

    array[high] ,array = array, array[high]
    return i



    def Quick_sort(array, low=0, high=None):
    if high is None:
    high = len(array) -1

    if low < high:
    pivot_ind = partition(array, low, high)

    Quick_sort(array, low, pivot_ind -1)
    Quick_sort(array, pivot_ind+1, high)



    Quick_sort(my_array)
    print(my_array)

    Continue reading...

Compartilhe esta Página