Optimize merge sort, quick sort, and knapsack algorithms#14419
Closed
DhruvrajSinhZala24 wants to merge 1 commit intoTheAlgorithms:masterfrom
Closed
Optimize merge sort, quick sort, and knapsack algorithms#14419DhruvrajSinhZala24 wants to merge 1 commit intoTheAlgorithms:masterfrom
DhruvrajSinhZala24 wants to merge 1 commit intoTheAlgorithms:masterfrom
Conversation
- sorts/merge_sort.py: Replace O(n) pop(0) calls in merge() with O(1) index-based traversal, fixing overall complexity from O(n² log n) to the correct O(n log n). Added type hints and comprehensive doctests. - sorts/quick_sort.py: Fix input mutation bug where collection.pop() destroyed the caller's original list. Use median-of-three pivot selection with three-way partitioning for better worst-case behavior on sorted/nearly-sorted inputs. Added type hints and doctests. - dynamic_programming/knapsack.py: Remove fragile global state from mf_knapsack() by passing the memoization table explicitly. Added type hints and doctests to knapsack() and mf_knapsack(). Added new knapsack_optimized() function with O(W) space complexity using a 1-D rolling array.
Closing this pull request as invalid@DhruvrajSinhZala24, this pull request is being closed as none of the checkboxes have been marked. It is important that you go through the checklist and mark the ones relevant to this pull request. Please read the Contributing guidelines. If you're facing any problem on how to mark a checkbox, please read the following instructions:
NOTE: Only |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR optimizes three algorithm implementations with bug fixes, performance improvements, and code quality enhancements.
Changes
1.
sorts/merge_sort.py- Fix O(n2) merge performancemerge()helper usedleft.pop(0)andright.pop(0), which are O(n) operations on Python lists, degrading overall sort complexity from O(n log n) to O(n2 log n).2.
sorts/quick_sort.py- Fix input mutation & improve pivot selectioncollection.pop(pivot_index)mutated the caller's original list - a hidden side effect.pop()entirely; uses list comprehensions on unmodified input.3.
dynamic_programming/knapsack.py- Remove global state & add space-optimized variantmf_knapsack()relied on aglobal ftable - fragile and not thread-safe.None).knapsack_optimized()function using O(W) space via 1-D rolling array.Verification
ruff check- 0 issues.blackformatting - clean.