Skip to content
The Chat Archive
claude-sonnet-4-20250514-thinking-32k· Anthropic LMArena Human Preference 140K

Write a python script to sort an array of numbers as fast as possible.

  1. User

    Write a python script to sort an array of numbers as fast as possible.

  2. Assistant

    Here's a Python script with the fastest sorting approaches:

    import random
    import time
    
    def fastest_sort_builtin(arr):
        """Fastest general-purpose sorting using Python's built-in sort"""
        return sorted(arr)
        # Alternative: arr.sort() for in-place sorting
    
    def fastest_sort_inplace(arr):
        """Fastest in-place sorting"""
        arr.sort()
        return arr
    
    def radix_sort(arr):
        """Fastest for non-negative integers with limited range"""
        if not arr:
            return arr
        
        max_num = max(arr)
        exp = 1
        
        while max_num // exp > 0:
            counting_sort_by_digit(arr, exp)
            exp *= 10
        
        return arr
    
    def counting_sort_by_digit(arr, exp):
        """Helper function for radix sort"""
        n = len(arr)
        output = [0] * n
        count = [0] * 10
        
        for i in range(n):
            index = arr[i] // exp
            count[index % 10] += 1
        
        for i in range(1, 10):
            count[i] += count[i - 1]
        
        i = n - 1
        while i >= 0:
            index = arr[i] // exp
            output[count[index % 10] - 1] = arr[i]
            count[index % 10] -= 1
            i -= 1
        
        for i in range(n):
            arr[i] = output[i]
    
    def counting_sort(arr):
        """Fastest for small range of non-negative integers"""
        if not arr:
            return arr
        
        max_val = max(arr)
        min_val = min(arr)
        range_val = max_val - min_val + 1
        
        count = [0] * range_val
        output = [0] * len(arr)
        
        for num in arr:
            count[num - min_val] += 1
        
        for i in range(1, range_val):
            count[i] += count[i - 1]
        
        for i in range(len(arr) - 1, -1, -1):
            output[count[arr[i] - min_val] - 1] = arr[i]
            count[arr[i] - min_val] -= 1
        
        return output
    
    # Example usage and benchmarking
    if __name__ == "__main__":
        # Generate test data
        test_sizes = [1000, 10000, 100000]
        
        for size in test_sizes:
            print(f"\nTesting with {size} elements:")
            
            # Random integers
            arr = [random.randint(1, 1000) for _ in range(size)]
            
            # Test built-in sort (usually fastest)
            arr_copy = arr.copy()
            start = time.time()
            result = fastest_sort_builtin(arr_copy)
            builtin_time = time.time() - start
            print(f"Built-in sort: {builtin_time:.6f} seconds")
            
            # Test in-place sort
            arr_copy = arr.copy()
            start = time.time()
            arr_copy.sort()
            inplace_time = time.time() - start
            print(f"In-place sort: {inplace_time:.6f} seconds")
            
            # Test counting sort (for small range integers)
            if max(arr) - min(arr) < size:  # Only if range is reasonable
                arr_copy = arr.copy()
                start = time.time()
                result = counting_sort(arr_copy)
                counting_time = time.time() - start
                print(f"Counting sort: {counting_time:.6f} seconds")
    
    # Quick sorting function - use this for maximum speed
    def quick_sort(arr):
        """Use this function for fastest general sorting"""
        return sorted(arr)
    
    # Example usage:
    numbers = [64, 34, 25, 12, 22, 11, 90, 5]
    sorted_numbers = quick_sort(numbers)
    print("Original:", numbers)
    print("Sorted:", sorted_numbers)
    

    For maximum speed, use:

    # Fastest for most cases
    sorted_array = sorted(your_array)
    
    # Or for in-place sorting (saves memory)
    your_array.sort()
    

    Key points:

    • Python's built-in sorted() is almost always the fastest option because it uses Timsort (implemented in C), which is highly optimized and adaptive
    • In-place sorting with list.sort() is slightly faster and uses less memory
    • Counting sort can be faster for small ranges of integers (when range < array length)
    • Radix sort can be faster for non-negative integers with limited digits

    For 99% of use cases, just use sorted(arr) or arr.sort() - they're incredibly fast and handle all edge cases beautifully.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.