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