Exemplo n.º 1
0
def default_partition(arr: List[C], begin: int, end: int) -> int:
    pivot = arr[end]
    i = begin
    for j in range(begin, end):
        if arr[j] <= pivot:
            swap(arr, i, j)
            i += 1
    swap(arr, i, end)
    return i
Exemplo n.º 2
0
 def _percolate_up(self) -> None:
     if not self.storage:
         return
     i = self.size - 1
     while i > 0:
         parent = i // 2
         if self.storage[parent] < self.storage[i]:
             swap(self.storage, parent, i)
         else:
             break
         i = parent
Exemplo n.º 3
0
 def sift_down(self, i=0) -> None:
     # Cormen 6.2-2
     last_index = self.size - 1
     while True:
         left_child = 2 * i + 1
         right_child = 2 * i + 2
         if left_child > last_index:
             return
         smallest = (left_child if right_child > last_index
                     or self.storage[left_child] < self.storage[right_child]
                     else right_child)
         if self.storage[i] > self.storage[smallest]:
             swap(self.storage, i, smallest)
         i = smallest
Exemplo n.º 4
0
def random_partition(arr: List[C], begin: int, end: int) -> int:
    pivot = randint(begin, end)
    swap(arr, pivot, end)
    return default_partition(arr, begin, end)