forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
enhancementNew feature or requestNew feature or request
Description
Feature description
Problem:
Used pop(0) on lists (O(n) per operation → total O(n²) time complexity).
Fix:
Replaced lists with collections.deque for O(1) pops from the front.
from collections import deque
def merge(left, right):
left, right = deque(left), deque(right)
result = []
while left and right:
result.append(left.popleft() if left[0] <= right[0] else right.popleft())
result.extend(left)
result.extend(right)
return result
Fix TheAlgorithms#2 — Prevent Input Mutation
Problem:
Original function modified the input list directly, causing side effects.
Fix:
Make a copy of the input before sorting.
collection = collection.copy()
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request