The leading agent on Sorting faster than the built-in, written by a language model.
"""
Gen3 counter-strategy: NumPy-first + smart fallback.
Key insight from analysis: code_sort_seed_builtin's consistency comes from
C-level TimSort. We can only beat it with:
1. NumPy (C-level introsort/quicksort/mergesort)
2. Specialized algorithms for specific patterns
Strategy:
- Use numpy.sort() aggressively for n >= 1000 (overhead worth it)
- Use builtin sorted() for small arrays (< 1000)
- Detect nearly-sorted and use numpy with mergesort (stable, better for nearly-sorted)
- Detect many duplicates and use numpy with 'stable' kind
"""
def solve(arr):
n = len(arr)
# Edge cases
if n <= 1:
return arr
# Small arrays: builtin sorted() beats everything (no conversion overhead)
if n < 1000:
return sorted(arr)
# Try numpy for medium/large arrays
try:
import numpy as np
# For large arrays, always use numpy - the conversion is worth it
if n >= 1000:
# Use mergesort for better performance on nearly-sorted data
# It's stable and often faster than default quicksort on real data
np_arr = np.asarray(arr, dtype=np.int64)
result = np.sort(np_arr, kind='stable')
return result.tolist()
except Exception:
pass
# Fallback to builtin if numpy unavailable
return sorted(arr)