Skip to content

Optimize merge sort, quick sort, and knapsack algorithms#14419

Closed
DhruvrajSinhZala24 wants to merge 1 commit intoTheAlgorithms:masterfrom
DhruvrajSinhZala24:optimize-merge-quick-sort-and-knapsack
Closed

Optimize merge sort, quick sort, and knapsack algorithms#14419
DhruvrajSinhZala24 wants to merge 1 commit intoTheAlgorithms:masterfrom
DhruvrajSinhZala24:optimize-merge-quick-sort-and-knapsack

Conversation

@DhruvrajSinhZala24
Copy link

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 performance

  • Bug: The merge() helper used left.pop(0) and right.pop(0), which are O(n) operations on Python lists, degrading overall sort complexity from O(n log n) to O(n2 log n).
    • Fix: Replaced with index-based two-pointer traversal for O(n) merge.
    • Added type hints and comprehensive doctests (12 tests).

2. sorts/quick_sort.py - Fix input mutation & improve pivot selection

  • Bug: collection.pop(pivot_index) mutated the caller's original list - a hidden side effect.
    • Fix: Eliminated pop() entirely; uses list comprehensions on unmodified input.
    • Replaced random pivot with median-of-three pivot selection with three-way partitioning.
    • Added type hints and doctests (16 tests), including a test verifying the original list is not mutated.

3. dynamic_programming/knapsack.py - Remove global state & add space-optimized variant

  • Bug: mf_knapsack() relied on a global f table - fragile and not thread-safe.
    • Fix: Memoization table now passed as explicit parameter (auto-created when None).
    • Added full type hints and doctests to all functions.
    • Added new knapsack_optimized() function using O(W) space via 1-D rolling array.

Verification

  • All 45 doctests pass across the 3 files.
    • ruff check - 0 issues.
    • black formatting - clean.

- 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.
@algorithms-keeper
Copy link

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:

  • Read a point one at a time and think if it is relevant to the pull request or not.
  • If it is, then mark it by putting a x between the square bracket like so: [x]

NOTE: Only [x] is supported so if you have put any other letter or symbol between the brackets, that will be marked as invalid. If that is the case then please open a new pull request with the appropriate changes.

@algorithms-keeper algorithms-keeper bot added awaiting reviews This PR is ready to be reviewed and removed awaiting reviews This PR is ready to be reviewed labels Mar 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant