MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / _swap

Method _swap

data_structures/heap/heap_generic.py:35–43  ·  view source on GitHub ↗

Performs changes required for swapping two elements in the heap

(self, i: int, j: int)

Source from the content-addressed store, hash-verified

33 return right if 0 < right < self.size else None
34
35 def _swap(self, i: int, j: int) -> None:
36 """Performs changes required for swapping two elements in the heap"""
37 # First update the indexes of the items in index map.
38 self.pos_map[self.arr[i][0]], self.pos_map[self.arr[j][0]] = (
39 self.pos_map[self.arr[j][0]],
40 self.pos_map[self.arr[i][0]],
41 )
42 # Then swap the items in the list.
43 self.arr[i], self.arr[j] = self.arr[j], self.arr[i]
44
45 def _cmp(self, i: int, j: int) -> bool:
46 """Compares the two items using default comparison"""

Callers 2

_heapify_upMethod · 0.95
_heapify_downMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected