diff --git a/sort.py b/sort.py index 402e60c..839bc35 100644 --- a/sort.py +++ b/sort.py @@ -12,17 +12,55 @@ class Sorter: @staticmethod def merge(data: List[Any], comparator: Callable[[Any, Any], bool]) -> List[Any]: - """ - Sorts the list using the merge sort algorithm. + # Copy of the data + arr = data[:] - Args: - data (List[Any]): The list to sort. - comparator (Callable[[Any, Any], bool]): Comparison function. + # Helper function to merge two sorted sublists + def merge_helper(left: List[Any], right: List[Any]) -> List[Any]: + result = [] + i = j = 0 - Returns: - List[Any]: A new sorted list. - """ - pass + # Merge elements from both lists while comparing with the comparator + while i < len(left) and j < len(right): + if comparator(left[i], right[j]): + result.append(left[i]) + i += 1 + else: + result.append(right[j]) + j += 1 + + # Append remaining elements from left sublist (if any) + while i < len(left): + result.append(left[i]) + i += 1 + + # Append remaining elements from right sublist (if any) + while j < len(right): + result.append(right[j]) + j += 1 + + return result + + # Recursive merge sort function + def merge_sort_recursive(arr: List[Any]) -> List[Any]: + # Base case: lists with 0 or 1 element are already sorted + if len(arr) <= 1: + return arr + + # Divide the list into two halves + mid = len(arr) // 2 + left = arr[:mid] + right = arr[mid:] + + # Recursively sort both halves + left_sorted = merge_sort_recursive(left) + right_sorted = merge_sort_recursive(right) + + # Merge the sorted halves + return merge_helper(left_sorted, right_sorted) + + # Perform merge sort and return the result + return merge_sort_recursive(arr) @staticmethod def insertion(data: List[Any], comparator: Callable[[Any, Any], bool]) -> List[Any]: