Signature
def sort(a):
Test Code
import random
from collections import Counter
def is_sorted(values):
return all(a <= b for a, b in zip(values, values[1:]))
for r in [10, 2**63 - 1]:
for length in range(200):
# Generate some random values in range [-r, r]
values = [random.randint(-r, r) for _ in range(length)]
old_values = list(values)
sort(values)
assert is_sorted(values), f"Values not sorted: {values}"
# Count of values may not change
assert Counter(values) == Counter(old_values), f"Values went missing.\nBefore: {old_values}\nAfter: {values}"
Description
Sort an array of integers in-place. Values range from -9223372036854775807 to 9223372036854775807.
Submit a Solution
Fastest Solutions
def sort(a): # in-place Timsort-based impl. (CPython 3.12)
"""
Sort integers in-place, handling any value permitted by the Python int type
(the function relies on Python’s native Timsort: O(n log n) and stable).
"""
a.sort()
def sort(a): # in-place Timsort used by Python lists
a.sort()
New Solutions
def sort(a): # in-place Timsort-based impl. (CPython 3.12)
"""
Sort integers in-place, handling any value permitted by the Python int type
(the function relies on Python’s native Timsort: O(n log n) and stable).
"""
a.sort()
def sort(a): # in-place Timsort used by Python lists
a.sort()