Skip to content

sort.sort #6

Description

@lindelwa122

Implement Sort Dispatcher Method

Description

Implement the sort() method in the Sorter class (sort.py). This method acts as a dispatcher that delegates to the appropriate sorting algorithm based on the method parameter.

Requirements

Method Signature:

@staticmethod
def sort(data: List[Any], comparator: Callable[[Any, Any], bool], method: str = "merge") -> List[Any]

Parameters:

  • data: A list of elements to sort
  • comparator: A function that takes two elements and returns True if the first should come before the second
  • method: The sorting algorithm to use - "merge", "insertion", or "bubble" (default: "merge")

Returns:

  • A new sorted list using the specified algorithm

Algorithm Details

This is a dispatcher/facade method that:

  1. Accepts a method string parameter
  2. Normalises it to lowercase
  3. Calls the appropriate sorting method (merge(), insertion(), or bubble())
  4. Raises ValueError if an unknown method is provided

Implementation Guidelines

  1. Convert method to lowercase for case-insensitive matching
  2. Delegate to existing methods:
    • "merge" → call Sorter.merge()
    • "insertion" → call Sorter.insertion()
    • "bubble" → call Sorter.bubble()
  3. Raise ValueError with a helpful message for unknown methods
  4. Default to merge sort when no method is specified
  5. Handle edge cases:
    • Empty method string (should raise ValueError)
    • Mixed case method names (e.g., "MeRgE")
    • Invalid method names

Testing

Run the test suite to verify your implementation:

python3 -m colorful_test test_sort.py

All tests related to sort() must pass, including:

  • Correct dispatching to merge, insertion, and bubble sort
  • Case-insensitive method names
  • Default behaviour (merge sort)
  • ValueError for invalid methods
  • ValueError for empty string

Example Usage

from sort import Sorter

data = [5, 2, 8, 1, 9]

# Using different methods
sorted_merge = Sorter.sort(data, lambda a, b: a < b, "merge")
sorted_insertion = Sorter.sort(data, lambda a, b: a < b, "insertion")
sorted_bubble = Sorter.sort(data, lambda a, b: a < b, "bubble")

# Default (merge sort)
sorted_default = Sorter.sort(data, lambda a, b: a < b)

# Case insensitive
sorted_mixed = Sorter.sort(data, lambda a, b: a < b, "MERGE")

# All produce: [1, 2, 5, 8, 9]

Error Handling

# Should raise ValueError
try:
    Sorter.sort(data, lambda a, b: a < b, "quicksort")
except ValueError as e:
    print(e)  # Unknown sort method 'quicksort'. Choose 'merge', 'insertion', or 'bubble'.

Acceptance Criteria

  • Method correctly dispatches to merge, insertion, and bubble sort
  • Case-insensitive method name handling
  • Defaults to merge sort when method not specified
  • Raises ValueError with descriptive message for invalid methods
  • All tests pass (24+ tests)
  • Code is clean and well-commented

Dependencies

This method depends on:

  • Sorter.merge() - must be implemented first
  • Sorter.insertion() - must be implemented first
  • Sorter.bubble() - must be implemented first

Note: You may want to implement the individual sorting methods before tackling this dispatcher method.


Note: Do not modify the test files. Ensure your implementation passes all existing tests.

Metadata

Metadata

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions