TestDrivenCode.com
1

Sort an array of numbers

by Tom
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

def sort(a):

Fastest Solutions

0
Solution by Anonymous User #29 (0.3 seconds)
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()
-1
Solution by Anonymous User #13 (0.3 seconds)
def sort(a): # in-place Timsort used by Python lists a.sort()
1
Solution by Tom (0.9 seconds)
def sort(a): for i in range(len(a)): for j in range(len(a)): if a[i] < a[j]: swap(a, i, j) def swap(a, i, j): tmp = a[i] a[i] = a[j] a[j] = tmp

New Solutions

0
Solution by Anonymous User #29 (0.3 seconds)
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()
-1
Solution by Anonymous User #13 (0.3 seconds)
def sort(a): # in-place Timsort used by Python lists a.sort()
1
Solution by Tom (0.9 seconds)
def sort(a): for i in range(len(a)): for j in range(len(a)): if a[i] < a[j]: swap(a, i, j) def swap(a, i, j): tmp = a[i] a[i] = a[j] a[j] = tmp