Skip to content

Conversation

@MathewAddala
Copy link

Description

This PR refactors the quick_sort function to use an in-place sorting approach, significantly improving space complexity and making it more aligned with standard quicksort implementations.

Problem

The current implementation:

  • Creates new lists (lesser and greater) during partitioning using list comprehensions
  • Uses collection.pop(pivot_index) to remove the pivot
  • Returns a new sorted list instead of sorting in-place
  • Has O(n) space complexity due to creating new lists at each recursion level

Solution

Refactored to use in-place sorting:

  • Introduced a helper function _quick_sort(collection, low, high) that performs recursive in-place sorting
  • Implemented _partition(collection, low, high) using the Lomuto partition scheme
  • Uses in-place swapping instead of creating new lists
  • The main quick_sort(collection) function now sorts the list in-place and returns it
  • Still uses random pivot selection for better average-case performance

Benefits

  • Space Complexity: Reduced from O(n) to O(log n) (only recursion stack)
  • Memory Efficiency: No allocation of new lists during partitioning
  • Performance: Better cache locality due to in-place operations
  • Time Complexity: Maintains O(n log n) average case
  • Standard Implementation: More aligned with canonical quicksort algorithms

Technical Details

The implementation uses the Lomuto partition scheme:

  1. Randomly select a pivot and swap it with the last element
  2. Maintain an index i that tracks the boundary of elements ≤ pivot
  3. Iterate through the array, swapping smaller elements to the left partition
  4. Place the pivot in its final position
  5. Recursively sort left and right partitions

Describe your change:

  • Fix a bug or typo in an existing algorithm? (Space complexity improvement)

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file.
  • All functions have doctests that pass the automated testing.

…xity

This commit refactors the quick_sort function to use an in-place sorting approach instead of creating new lists during the partitioning process.

**Changes made:**
- Introduced a helper function `_quick_sort(collection, low, high)` that performs the recursive in-place sorting
- Implemented `_partition(collection, low, high)` using the Lomuto partition scheme for in-place partitioning
- The main `quick_sort(collection)` function now sorts the list in-place and returns it
- Removed the list comprehensions that created new `lesser` and `greater` lists
- Replaced `collection.pop()` with in-place swapping

**Benefits:**
- Improved space complexity: O(log n) for the recursion stack instead of O(n) for creating new lists
- More memory-efficient, especially for large collections
- Maintains the same O(n log n) average time complexity
- Still uses random pivot selection for better average-case performance
@algorithms-keeper
Copy link

Multiple Pull Request Detected

@MathewAddala, we are extremely excited that you want to submit multiple algorithms in this repository but we have a limit on how many pull request a user can keep open at a time. This is to make sure all maintainers and users focus on a limited number of pull requests at a time to maintain the quality of the code.

This pull request is being closed as the user already has an open pull request. Please focus on your previous pull request before opening another one. Thank you for your cooperation.

User opened pull requests (including this one): #13838, #13836, #13821, #13818

@algorithms-keeper algorithms-keeper bot closed this Nov 1, 2025
@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Nov 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant