def test_partitioned_mixed_array(self): """ Ensure that the array D, a mixed sequence, is partitioned. """ D = [3, 5, 4, 1, 2] # Pivot element 3 DPartition = [2, 1, 3, 5, 4] DPrime, k = qS.partition(D, 0, 4) self.assertEqual(DPartition, DPrime) self.assertEqual(2, k)
def test_partitioned_descending_array(self): """ Ensure that the array C, a descending sequence, is partitioned. """ C = [100, 5, 4, 3, 2, 1, 1000] # Pivot element 5 CPartition = [100, 1, 4, 3, 2, 5, 1000] CPrime, k = qS.partition(C, 1, 5) self.assertEqual(CPartition, CPrime) self.assertEqual(5, k)
def introSortRec(array, start, end, maxDepth): if end - start > 1: array, pivot = partition(array, start, end) if maxDepth == 0: heapSort(array, start, end) elif start < end: introSortRec(array, start, pivot, maxDepth - 1) introSortRec(array, pivot + 1, end, maxDepth - 1)
def introSort(array, begin=0, end=None, depth=0, *, reverse=False): if end is None: end = len(array) - 1 if depth < log2(len(array)): if begin < end: mid = partition(array, begin, end, reverse=reverse) introSort(array, begin, mid - 1, depth + 1, reverse=reverse) introSort(array, mid + 1, end, depth + 1, reverse=reverse) else: array[begin:end + 1] = heapSort(array[begin:end + 1], reverse=reverse)
def recursiveQuickSelectRand(l, left, right, k): if left > right: # controllo superfluo return if left == right and k - 1 == left: return l[k - 1] mid = partition(l, left, right) if k - 1 == mid: return l[mid] if k - 1 < mid: return recursiveQuickSelectRand(l, left, mid - 1, k) else: return recursiveQuickSelectRand(l, mid + 1, right, k)
def test_partitioned_ascending_array(self): """ Ensure that the array B, an ascending sequence, is partitioned. """ B = [100, 1, 2, 3, 4, 5, 1000] # Pivot element 1 BPrime, k = qS.partition(B, 1, 5) self.assertEqual(B, BPrime) self.assertEqual(1, k)