def partition(items, left, right, pivot_index, compare):
    """Groups items between left and right index into two parts:
    those less than element at pivot_index, and those greater than or equal to it
    """
    assert 0 <= left <= pivot_index <= right < len(items)
    if left == right:
        return pivot_index

    pivot_value = items[pivot_index]
    items[right], items[pivot_index] = items[pivot_index], items[right]
    pivot_index = left
    for i in range(left, right):
        if compare(items[i], pivot_value):
            items[pivot_index], items[i] = items[i], items[pivot_index]
            pivot_index += 1
    items[right], items[pivot_index] = items[pivot_index], items[right]
    return pivot_index