Commit 009090e
committed
refactor(core): Optimize and clarify _pdqSiftDown in heapsort
This commit refactors the _pdqSiftDown helper function, which is used as part of the heapsort fallback mechanism within the quickSort implementation.
The changes improve both performance and readability:
1- Key Caching: The key of the current largest element is now cached in a local variable (largestKey). This avoids multiple redundant calls to the keyOf function within the loop, reducing overhead.
2- Clearer Logic: The comparison logic has been restructured. Instead of potentially re-evaluating keyOf(elements[start + largest]) after the first comparison, the cached key is used, and the flow for comparing the root with its left and right children is now more explicit and easier to follow.
3- Early Exit: An early break is introduced for leaf nodes (when left >= n), which slightly simplifies the control flow.
These changes do not alter the algorithm's behavior but make the implementation more efficient and maintainable.1 parent 307ec48 commit 009090e
1 file changed
+22
-15
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
578 | 578 | | |
579 | 579 | | |
580 | 580 | | |
581 | | - | |
582 | | - | |
| 581 | + | |
583 | 582 | | |
584 | | - | |
585 | | - | |
586 | | - | |
587 | | - | |
588 | | - | |
589 | | - | |
590 | | - | |
591 | | - | |
592 | | - | |
593 | | - | |
594 | | - | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
595 | 592 | | |
596 | | - | |
597 | | - | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
598 | 602 | | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
599 | 606 | | |
600 | 607 | | |
601 | 608 | | |
| |||
0 commit comments