* @zh 下沉操作 * @en Sink down operation
(index: number)
| 127 | * @en Sink down operation |
| 128 | */ |
| 129 | private sinkDown(index: number): void { |
| 130 | const length = this.heap.length; |
| 131 | const item = this.heap[index]; |
| 132 | |
| 133 | while (true) { |
| 134 | const leftIndex = 2 * index + 1; |
| 135 | const rightIndex = 2 * index + 2; |
| 136 | let smallest = index; |
| 137 | |
| 138 | if (leftIndex < length && this.compare(this.heap[leftIndex], this.heap[smallest]) < 0) { |
| 139 | smallest = leftIndex; |
| 140 | } |
| 141 | |
| 142 | if (rightIndex < length && this.compare(this.heap[rightIndex], this.heap[smallest]) < 0) { |
| 143 | smallest = rightIndex; |
| 144 | } |
| 145 | |
| 146 | if (smallest === index) { |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | this.heap[index] = this.heap[smallest]; |
| 151 | this.heap[smallest] = item; |
| 152 | index = smallest; |
| 153 | } |
| 154 | } |
| 155 | } |