Beispiel #1
0
def quick_sort(left, right, array):
    if left < right and (right - left + 1) > 10:
        split_point = qsort.partition(array, left, right)
        array = quick_sort(left, split_point - 1, array)
        array = quick_sort(split_point + 1, right, array)
    elif left < right:
        array[left:right + 1] = insertion_sort(array[left:right + 1])
    return array
Beispiel #2
0
def order(A, p, r, i):
    '''Return the i-th smallest element of A[p:r+1].'''

    if p == r:
        return A[p]

    q = partition(A, p, r)
    k = q - p + 1

    if i <= k:
        return order(A, p, q, i)
    else:
        return order(A, q + 1, r, i - k)
Beispiel #3
0
def ksel(x, left, right, k):
    if right == left and k == 0:
        return x[left]
    if right == left + 1:
        if k == 0:
            return min(x[left], x[right])
        elif k == 1:
            return max(x[left], x[right])
    p_idx = left + (right - left) / 2
    p_idx_new = partition(x, left, right, p_idx)
    if k == p_idx_new - left + 1:
        return x[p_idx_new]
    elif k < p_idx_new - left + 1:
        return ksel(x, left, p_idx_new - 1, k)
    else:
        return ksel(x, p_idx_new + 1, right, k - p_idx_new)