def partition(list_, begin, end, pivotIndex):
    """
    Partition a sublist around a pivot and return the final pivot index.
    
    list_ -- A list of items that can be compared using the less than or 
             equal (<=) operator.
    begin -- The index of the first of the items we care about.
    end -- The index just past the last of the items we care about.
    pivotIndex -- the (initial) index of the pivot
    
    Partition the sublist list_[begin:end] into three parts:
    - left,  which contains all items less or equal to the pivot,
    - pivot, the pivot item itself, and
    - right, which contains all items greater than or equal to the pivot
    """
    pivot = list_[pivotIndex]
    # move pivot to the end
    list_[pivotIndex], list_[end - 1] = list_[end - 1], list_[pivotIndex]
    # storeIndex will point to the first element that is greater than or
    # equal to the pivot (initially points to the leftmost element)
    storeIndex = begin
    for i in range(begin, end - 1):
        if list_[i] < pivot:
            list_[i], list_[storeIndex] = list_[storeIndex], list_[i]
            storeIndex += 1
    # now everything before storeIndex is less than the pivot, and storeIndex
    # points to the first item that is greater than or equal to the pivot;
    # move pivot back to where storeIndex points
    list_[storeIndex], list_[end - 1] = list_[end - 1], list_[storeIndex]
    return storeIndex