|
1 |
| -def merge(self, left, right): |
| 1 | +from typing import List |
| 2 | + |
| 3 | + |
| 4 | +def merge(left: List[int], right: List[int]) -> List[int]: |
| 5 | + """Merges two sorted lists into a single sorted list. |
| 6 | +
|
| 7 | + Args: |
| 8 | + left (list): The left half of the list to be merged. |
| 9 | + right (list): The right half of the list to be merged. |
| 10 | +
|
| 11 | + Returns: |
| 12 | + list: The sorted list resulting from merging the left and right halves. |
| 13 | + """ |
2 | 14 | if len(left) == 0:
|
3 | 15 | return right
|
4 | 16 |
|
@@ -26,23 +38,20 @@ def merge(self, left, right):
|
26 | 38 | return result
|
27 | 39 |
|
28 | 40 |
|
29 |
| -def mergeSort(self, arr): |
30 |
| - """ mergeSort Algorithm Implementation in Python 3 |
| 41 | +def merge_sort(unsorted_list: List[int]) -> List[int]: |
| 42 | + """Sorts a list of integers using the merge sort algorithm. |
31 | 43 |
|
32 |
| - arr : Unorded list |
33 |
| - output : Return list in ascending order. |
34 |
| - time complexity : O(n log2n) |
35 |
| - Note : O(n log2n) is the best possible worst-case runtime that can be |
36 |
| - achieved by a sorting algorithm. |
| 44 | + Args: |
| 45 | + unsorted_list (list): The unsorted list to be sorted. |
37 | 46 |
|
38 |
| - Example : |
39 |
| - >>> mergeSort([4,2,6,5,9,8]) |
40 |
| - [2, 4, 5, 6, 8, 9]""" |
| 47 | + Returns: |
| 48 | + list: The sorted list. |
| 49 | + """ |
| 50 | + if len(unsorted_list) < 2: |
| 51 | + return unsorted_list |
41 | 52 |
|
42 |
| - if len(arr) < 2: |
43 |
| - return arr |
| 53 | + midpoint = len(unsorted_list) // 2 |
44 | 54 |
|
45 |
| - midpoint = len(arr) // 2 |
| 55 | + return merge(left=merge_sort(unsorted_list[:midpoint]), |
| 56 | + right=merge_sort(unsorted_list[midpoint:])) |
46 | 57 |
|
47 |
| - return merge(left=mergeSort(arr[:midpoint]), |
48 |
| - right=mergeSort(arr[midpoint:])) |
|
0 commit comments