dsa.sorttools

Module to access functions for sort benchmarking.

 1""" Module to access functions for sort benchmarking. """
 2import random
 3
 4def rand_int_array(n: int, maxnum: int) -> list:
 5    """ 
 6    Return an array of n integers of random numbers from 0 to maxnum.
 7
 8    Args:
 9        n (int): The number of integers to generate.
10        maxnum (int): The maximum number in a range (0-maxnum inclusive).
11    Returns:
12        Array of n integers of random numbers from 0 to maxnum.
13    """
14    array = [None] * n
15    for i in range(n):
16        array[i] = random.randint(0, maxnum)
17    return array
18
19def filled_array(n: int) -> list:
20    """ 
21    Return an array filled with integers from 0 to n-1.
22
23    Args:
24        n (int): the number of integers to generate.
25
26    Returns:
27        Array filled with integers from 0 to n-1.
28    """
29    array = [None] * n
30    for i in range(n):
31        array[i] = i
32    return array    
33
34def shuffle_array(n: int) -> list:
35    """ 
36    Return a shuffled array filled with integers from 0 to n-1.
37
38    Args:
39        n (int): The number of integers to generate.
40    Returns:
41        Array shuffled with integers from 0 to n-1.
42    """
43    array = filled_array(n)
44    for i in range(n):
45        r = random.randint(i, n-1)
46        array[i], array[r] = array[r], array[i]
47    return array
48
49def is_sorted(array: list) -> bool:
50    """ 
51    Return a boolean on whether an array is sorted in ascending order or not.
52
53    Args:
54        array: the array to verify.
55    Returns:
56        True if the array is sorted, False otherwise.   
57    """
58    for i in range(len(array)-1):
59        if array[i + 1] < array[i]:
60            return False
61    return True
62
63def array_details(array: list) -> str:
64    """
65    Return a string with details about the array.
66    Args:
67        array: the array to analyze.
68    Returns:
69        A string with the count of elements, first 10 elements, and last 10 elements.
70    """
71    return f"Count: {len(array)} first 10: {array[:10]} last 10: {array[-10:]}"
72
73def generate_almost_sorted_array(size: int, swaps: int) -> list:
74    """
75    Generate an almost sorted array of a given size with a specified number of swaps.   
76    Args:
77        size (int): The size of the array to generate.
78        swaps (int): The number of adjacent elements to swap to create disorder.
79    Returns:
80        list: An array of integers that is mostly sorted with a few local swaps.
81    """
82
83    arr = list(range(size))
84    for _ in range(swaps):
85        i = random.randint(0, size - 2)
86        arr[i], arr[i + 1] = arr[i + 1], arr[i]  # introduce a small local disorder
87    return arr
def rand_int_array(n: int, maxnum: int) -> list:
 5def rand_int_array(n: int, maxnum: int) -> list:
 6    """ 
 7    Return an array of n integers of random numbers from 0 to maxnum.
 8
 9    Args:
10        n (int): The number of integers to generate.
11        maxnum (int): The maximum number in a range (0-maxnum inclusive).
12    Returns:
13        Array of n integers of random numbers from 0 to maxnum.
14    """
15    array = [None] * n
16    for i in range(n):
17        array[i] = random.randint(0, maxnum)
18    return array

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.

def filled_array(n: int) -> list:
20def filled_array(n: int) -> list:
21    """ 
22    Return an array filled with integers from 0 to n-1.
23
24    Args:
25        n (int): the number of integers to generate.
26
27    Returns:
28        Array filled with integers from 0 to n-1.
29    """
30    array = [None] * n
31    for i in range(n):
32        array[i] = i
33    return array    

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:
35def shuffle_array(n: int) -> list:
36    """ 
37    Return a shuffled array filled with integers from 0 to n-1.
38
39    Args:
40        n (int): The number of integers to generate.
41    Returns:
42        Array shuffled with integers from 0 to n-1.
43    """
44    array = filled_array(n)
45    for i in range(n):
46        r = random.randint(i, n-1)
47        array[i], array[r] = array[r], array[i]
48    return array

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.

def is_sorted(array: list) -> bool:
50def is_sorted(array: list) -> bool:
51    """ 
52    Return a boolean on whether an array is sorted in ascending order or not.
53
54    Args:
55        array: the array to verify.
56    Returns:
57        True if the array is sorted, False otherwise.   
58    """
59    for i in range(len(array)-1):
60        if array[i + 1] < array[i]:
61            return False
62    return True

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.

def array_details(array: list) -> str:
64def array_details(array: list) -> str:
65    """
66    Return a string with details about the array.
67    Args:
68        array: the array to analyze.
69    Returns:
70        A string with the count of elements, first 10 elements, and last 10 elements.
71    """
72    return f"Count: {len(array)} first 10: {array[:10]} last 10: {array[-10:]}"

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.

def generate_almost_sorted_array(size: int, swaps: int) -> list:
74def generate_almost_sorted_array(size: int, swaps: int) -> list:
75    """
76    Generate an almost sorted array of a given size with a specified number of swaps.   
77    Args:
78        size (int): The size of the array to generate.
79        swaps (int): The number of adjacent elements to swap to create disorder.
80    Returns:
81        list: An array of integers that is mostly sorted with a few local swaps.
82    """
83
84    arr = list(range(size))
85    for _ in range(swaps):
86        i = random.randint(0, size - 2)
87        arr[i], arr[i + 1] = arr[i + 1], arr[i]  # introduce a small local disorder
88    return arr

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.