(heap, pos)
| 272 | # for sorting. |
| 273 | |
| 274 | def _siftup(heap, pos): |
| 275 | endpos = len(heap) |
| 276 | startpos = pos |
| 277 | newitem = heap[pos] |
| 278 | # Bubble up the smaller child until hitting a leaf. |
| 279 | childpos = 2*pos + 1 # leftmost child position |
| 280 | while childpos < endpos: |
| 281 | # Set childpos to index of smaller child. |
| 282 | rightpos = childpos + 1 |
| 283 | if rightpos < endpos and not heap[childpos] < heap[rightpos]: |
| 284 | childpos = rightpos |
| 285 | # Move the smaller child up. |
| 286 | heap[pos] = heap[childpos] |
| 287 | pos = childpos |
| 288 | childpos = 2*pos + 1 |
| 289 | # The leaf at pos is empty now. Put newitem there, and bubble it up |
| 290 | # to its final resting place (by sifting its parents down). |
| 291 | heap[pos] = newitem |
| 292 | _siftdown(heap, startpos, pos) |
| 293 | |
| 294 | def _siftdown_max(heap, startpos, pos): |
| 295 | 'Maxheap variant of _siftdown' |
no test coverage detected