def randomize_in_place_(A): n = A.length j = random(1, n) A[1], A[j] = A[j], A[1] for i in between(2, n): j = random(i, n) A[i], A[j] = A[j], A[i]
def permute_by_sorting(A): n = A.length P = Array.indexed(1, n) for i in between(1, n): P[i] = random(1, n ** 3) _sort_using_priorities(A, P) return A
def fair_tree_delete(T, z): if z.left is None or z.right is None: y = z else: if random(0, 1) == 0: y = tree_predecessor(z) else: y = tree_successor(z) if y.left is not None: x = y.left else: x = y.right if x is not None: x.p = y.p if y.p is None: T.root = x else: if y is y.p.left: y.p.left = x else: y.p.right = x if y is not z: z.key = y.key z.data = y.data return y
def permute_by_sorting(A): n = A.length P = Array.indexed(1, n) for i in between(1, n): P[i] = random(1, n**3) _sort_using_priorities(A, P) return A
def test_random(self): lower_bound = 10 upper_bound = 20 x = random(lower_bound, upper_bound) assert_that(x, is_(greater_than_or_equal_to(lower_bound))) assert_that(x, is_(less_than_or_equal_to(upper_bound)))
def compact_list_search(L, n, k): i = L.head while i is not None and L.key[i] < k: j = random(1, n) if L.key[i] < L.key[j] and L.key[j] <= k: i = j if L.key[i] == k: return i i = L.next[i] if i is None or L.key[i] > k: return None else: return i
def random_search(A, x): n = A.length B = Array.indexed(1, n) for k in between(1, n): B[k] = False checked = 0 while checked < n: i = random(1, n) if A[i] == x: return i if not B[i]: B[i] = True checked = checked + 1 return None
def fuzzy_partition(A, p, r): j = random(p, r) A[r], A[j] = A[j], A[r] x = A[r].low i = p - 1 for j in between(p, r - 1): if A[j].low <= x: i = i + 1 A[i], A[j] = A[j], A[i] A[i + 1], A[r] = A[r], A[i + 1] q = i + 1 for k in rbetween(i, p): if A[k].high >= x: q = q - 1 A[q], A[k] = A[k], A[q] return q, i + 1
def jugs_partition(R, B, p, r): j = random(p, r) R[r], R[j] = R[j], R[r] x = R[r] i = p while B[i] != x: i = i + 1 B[i], B[r] = B[r], B[i] i = p - 1 for j in between(p, r - 1): if B[j] < x: i = i + 1 B[i], B[j] = B[j], B[i] B[i + 1], B[r] = B[r], B[i + 1] i = p - 1 for j in between(p, r - 1): if R[j] < x: i = i + 1 R[i], R[j] = R[j], R[i] R[i + 1], R[r] = R[r], R[i + 1] return i + 1
def randomized_partition(A, p, r): i = random(p, r) A[r], A[i] = A[i], A[r] return partition(A, p, r)
def randomize_in_place(A): n = A.length for i in between(1, n): j = random(i, n) A[i], A[j] = A[j], A[i]
def randomize_in_place(A): n = A.length for i in between(1, n): j = random(i, n) A[i], A[j] = A[j], A[i]