Skip to content

Conversation

@balaraj74
Copy link
Owner

📝 Summary

This pull request implements the Median of Two Sorted Arrays algorithm using an efficient divide-and-conquer approach, addressing issue TheAlgorithms#13717.

🎯 What does this PR do?

Adds a new algorithm that finds the median of two sorted arrays in logarithmic time complexity. This is a classic computer science problem that demonstrates the power of the divide-and-conquer paradigm.

📂 Files Added

  • divide_and_conquer/median_of_two_sorted_arrays.py - Main implementation with comprehensive documentation

✨ Implementation Highlights

Algorithm Features:

  • Time Complexity: O(log(min(m, n))) - Very efficient using binary search partitioning
  • Space Complexity: O(1) - Constant extra space
  • Type Support: Works with both integers and floating-point numbers
  • Edge Cases Handled:
    • One or both arrays can be empty (with appropriate error handling)
    • Arrays of different lengths
    • Negative numbers
    • Duplicate values
    • Odd and even total element counts

How It Works:

The algorithm uses a partition-based approach:

  1. Always binary search on the smaller array for efficiency
  2. Find the correct partition point where elements on the left are ≤ elements on the right
  3. Calculate median based on whether total elements are odd or even

✅ Testing

The implementation includes 10 comprehensive doctests covering:

  • ✓ Basic cases (odd/even total lengths)
  • ✓ Empty array handling (one empty)
  • ✓ Both arrays empty (raises ValueError)
  • ✓ Floating-point numbers
  • ✓ Negative numbers
  • ✓ Duplicate values
  • ✓ Significantly different array sizes

All tests pass successfully!

python3 -m doctest divide_and_conquer/median_of_two_sorted_arrays.py -v
# Result: 10 passed and 0 failed

🔗 Related Issue

Closes TheAlgorithms#13717

📚 Code Quality

  • ✓ Type hints for all parameters and return values
  • ✓ Comprehensive docstring with examples
  • ✓ Clear variable names following Python conventions
  • ✓ Detailed inline comments explaining the algorithm
  • ✓ Follows repository coding standards
  • ✓ Snake_case naming convention

🚀 Why This Matters

This algorithm is:

  • A fundamental problem in computer science interviews
  • Demonstrates efficient divide-and-conquer thinking
  • Much more efficient than the naive O(m+n) merge approach
  • A great learning resource for the community

🙏 Acknowledgments

Thanks to @devdandekar24 for opening this issue and to the maintainers of TheAlgorithms/Python for maintaining this excellent learning resource!


Ready for review! Happy to make any adjustments based on feedback. 😊

- Implement partition-based divide-and-conquer solution
- Time complexity: O(log(min(m, n)))
- Space complexity: O(1)
- Handles empty arrays, integers, floats, and negative numbers
- Includes comprehensive doctests for edge cases
- Fixes TheAlgorithms#13717
@balaraj74 balaraj74 merged commit fd492e1 into master Oct 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Median of Two sorted arrays

1 participant