The sorttools module provides utilities for testing and benchmarking sorting algorithms. It includes functions to generate various types of arrays (random, sorted, shuffled, almost-sorted) and verify sorting correctness.
These utilities are particularly useful for algorithm analysis, performance testing, and understanding how different sorting algorithms perform on different input patterns.
def rand_int_array(n: int, maxnum: int) -> list: """ Return an array of n integers of random numbers from 0 to maxnum. Args: n (int): The number of integers to generate. maxnum (int): The maximum number in a range (0-maxnum inclusive). Returns: Array of n integers of random numbers from 0 to maxnum. """
Parameters:
n (int): The number of integers to generate
maxnum (int): The maximum value (range is 0 to maxnum inclusive)
Returns:
list: Array of n random integers
Example:
from dsa.sorttools import rand_int_array# Generate 100 random integers from 0 to 1000array = rand_int_array(100, 1000)print(array[:10]) # Show first 10 elements
Generates a sorted array with consecutive integers.
sorttools.py:20
def filled_array(n: int) -> list: """ Return an array filled with integers from 0 to n-1. Args: n (int): the number of integers to generate. Returns: Array filled with integers from 0 to n-1. """
def shuffle_array(n: int) -> list: """ Return a shuffled array filled with integers from 0 to n-1. Args: n (int): The number of integers to generate. Returns: Array shuffled with integers from 0 to n-1. """
Parameters:
n (int): The number of integers to generate
Returns:
list: Shuffled array containing 0 to n-1
Example:
from dsa.sorttools import shuffle_array# Generate shuffled array of 0-99array = shuffle_array(100)
Generates an array that is mostly sorted with a few local swaps.
sorttools.py:78
def generate_almost_sorted_array(size: int, swaps: int) -> list: """ Generate an almost sorted array of a given size with a specified number of swaps. Args: size (int): The size of the array to generate. swaps (int): The number of adjacent elements to swap to create disorder. Returns: list: An array of integers that is mostly sorted with a few local swaps. """
Parameters:
size (int): The size of the array to generate
swaps (int): The number of adjacent element swaps to introduce
Returns:
list: Array that is mostly sorted with local disorder
Example:
from dsa.sorttools import generate_almost_sorted_array# Generate array of 1000 elements with only 10 swapsarray = generate_almost_sorted_array(1000, 10)
This function is particularly useful for testing adaptive sorting algorithms like insertion sort, which perform better on nearly-sorted data.
Verifies if an array is sorted in ascending order.
sorttools.py:51
def is_sorted(array: list) -> bool: """ Return a boolean on whether an array is sorted in ascending order or not. Args: array: the array to verify. Returns: True if the array is sorted, False otherwise. """
Parameters:
array (list): The array to verify
Returns:
bool: True if sorted in ascending order, False otherwise
def array_details(array: list) -> str: """ Return a string with details about the array. Args: array: the array to analyze. Returns: A string with the count of elements, first 10 elements, and last 10 elements. """
Parameters:
array (list): The array to analyze
Returns:
str: String containing array size, first 10 elements, and last 10 elements
Example:
from dsa.sorttools import array_details, shuffle_arrayarray = shuffle_array(1000)print(array_details(array))# Output: Count: 1000 first 10: [42, 17, 99, ...] last 10: [..., 15, 8, 93]
from dsa.sorttools import rand_int_array, is_sorteddef test_sort_correctness(sort_func, num_tests=100): """Test sorting algorithm correctness""" for i in range(num_tests): size = (i + 1) * 100 data = rand_int_array(size, size * 10) sort_func(data) if not is_sorted(data): print(f"FAILED at size {size}") return False print(f"PASSED all {num_tests} tests") return True