|
| 1 | +# 912. Sort an Array |
| 2 | +# 🟠 Medium |
| 3 | +# |
| 4 | +# https://leetcode.com/problems/sort-an-array/ |
| 5 | +# |
| 6 | +# Tags: Array - Divide and Conquer - Sorting - Heap (Priority Queue) |
| 7 | +# - Merge Sort - Bucket Sort - Radix Sort - Counting Sort |
| 8 | + |
| 9 | +import timeit |
| 10 | +from typing import List |
| 11 | + |
| 12 | + |
| 13 | +# Given the conditions of the problem description, merge sort seems to |
| 14 | +# be a good option, it won't use any extra memory and it guarantees |
| 15 | +# O(n*log(n)) time complexity. |
| 16 | +# |
| 17 | +# Time complexity: O(n*log(n)) |
| 18 | +# Space complexity: O(n) - We use one copy of the input array to |
| 19 | +# alternately move elements from one to the other. The call stack will |
| 20 | +# be of height log(n), that is an extra O(log(n)). |
| 21 | +# |
| 22 | +# Runtime 1690 ms Beats 72.17% |
| 23 | +# Memory 21.4 MB Beats 90.91% |
| 24 | +class MergeSort: |
| 25 | + def sortArray(self, nums: List[int]) -> List[int]: |
| 26 | + # Define an internal function that sorts a section of the input |
| 27 | + # array to avoid passing copies of the array between calls. |
| 28 | + def mergeSort(a: List[int], b: List[int], l: int, r: int) -> None: |
| 29 | + # Base case, the array has only one element. |
| 30 | + if l == r: |
| 31 | + return |
| 32 | + mid = l + ((r - l) // 2) |
| 33 | + # Swap the function of each array in each call level. |
| 34 | + mergeSort(b, a, l, mid) |
| 35 | + mergeSort(b, a, mid + 1, r) |
| 36 | + merge(a, b, l, mid, r) |
| 37 | + |
| 38 | + def merge(destination, source, l, mid, r): |
| 39 | + # The halves are sorted, merge them. Define i and j, two |
| 40 | + # read pointers that read the next unused element in each of |
| 41 | + # the sorted halves, and one insert pointer of the index at |
| 42 | + # which we want to insert the next value in the result. |
| 43 | + i, j, ins = l, mid + 1, l |
| 44 | + while i <= mid and j <= r: |
| 45 | + if source[j] < source[i]: |
| 46 | + destination[ins] = source[j] |
| 47 | + j += 1 |
| 48 | + else: |
| 49 | + destination[ins] = source[i] |
| 50 | + i += 1 |
| 51 | + ins += 1 |
| 52 | + # Use up any remaining elements from either half. |
| 53 | + while i <= mid: |
| 54 | + destination[ins] = source[i] |
| 55 | + i += 1 |
| 56 | + ins += 1 |
| 57 | + while j <= r: |
| 58 | + destination[ins] = source[j] |
| 59 | + j += 1 |
| 60 | + ins += 1 |
| 61 | + |
| 62 | + # A copy of the input array that we use to alternate between |
| 63 | + # sorted and unsorted sections. |
| 64 | + helper = nums[:] |
| 65 | + mergeSort(nums, helper, 0, len(nums) - 1) |
| 66 | + return nums |
| 67 | + |
| 68 | + |
| 69 | +def test(): |
| 70 | + executors = [ |
| 71 | + MergeSort, |
| 72 | + ] |
| 73 | + tests = [ |
| 74 | + [[0], [0]], |
| 75 | + [[1, 0], [0, 1]], |
| 76 | + [[5, 2, 1], [1, 2, 5]], |
| 77 | + [[5, 2, 3, 1], [1, 2, 3, 5]], |
| 78 | + [[5, 1, 1, 2, 0, 0], [0, 0, 1, 1, 2, 5]], |
| 79 | + ] |
| 80 | + for executor in executors: |
| 81 | + start = timeit.default_timer() |
| 82 | + for _ in range(1): |
| 83 | + for col, t in enumerate(tests): |
| 84 | + sol = executor() |
| 85 | + result = sol.sortArray(t[0]) |
| 86 | + exp = t[1] |
| 87 | + assert result == exp, ( |
| 88 | + f"\033[93m» {result} <> {exp}\033[91m for" |
| 89 | + + f" test {col} using \033[1m{executor.__name__}" |
| 90 | + ) |
| 91 | + stop = timeit.default_timer() |
| 92 | + used = str(round(stop - start, 5)) |
| 93 | + cols = "{0:20}{1:10}{2:10}" |
| 94 | + res = cols.format(executor.__name__, used, "seconds") |
| 95 | + print(f"\033[92m» {res}\033[0m") |
| 96 | + |
| 97 | + |
| 98 | +test() |
0 commit comments