OccupyMars2025 / Introduction-to-Algorithms-4th-Edition

Time is Money, Efficiency is Life
MIT License
1 stars 0 forks source link

[TODO] new implementation for the PARTITION procedure in the QUICKSORT #4

Open OccupyMars2025 opened 6 months ago

OccupyMars2025 commented 6 months ago

def partition(arr: List, start: int, end: int) -> int:
    """
    Args:
        arr (List): the list to partition
        start (int): inclusive
        end (int): inclusive

    Returns:
        the pivot index
    """
    pivot_index = random.randint(start, end)
    pivot = arr[pivot_index]
    left = start
    right = end
    while left <= right:
        while arr[left] < pivot:
            left += 1
        while arr[right] > pivot:
            right -= 1
        if left <= right:
            arr[left], arr[right] = arr[right], arr[left]
            left += 1
            right -= 1
    return left

Chekc if this new implementation is wrong. If it is wrong, can we modify it to make it correct ?

refer to https://github.com/OccupyMars2025/Introduction-to-Algorithms-4th-Edition/blob/main/my_implementation/Chapter_07_Quicksort/quicksort/quicksort.py