diff --git a/sorts/merge_sort.py b/sorts/merge_sort.py index 0628b848b794..7bbbe46e6a46 100644 --- a/sorts/merge_sort.py +++ b/sorts/merge_sort.py @@ -37,10 +37,19 @@ def merge(left: list, right: list) -> list: :return: Merged result """ result = [] - while left and right: - result.append(left.pop(0) if left[0] <= right[0] else right.pop(0)) - result.extend(left) - result.extend(right) + left_index = right_index = 0 + length_left, length_right = len(left), len(right) + + while (left_index < length_left) and (right_index < length_right): + if left[left_index] < right[right_index]: + result.append(left[left_index]) + left_index += 1 + else: + result.append(right[right_index]) + right_index += 1 + + result.extend(left[left_index:]) + result.extend(right[right_index:]) return result if len(collection) <= 1: pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy