def selection_sort(array, ascending=True):
    """Sort array using selection sort algorithm.

    Parameters
    ----------
    array : list
        List to be sorted; Can contain any Python objects that can be compared
    ascending : bool, optional
        If True sort array from smallest to largest; False -> sort array from
        largest to smallest

    Returns
    -------
    list
        Input array sorted.
    """
    # Create copy to avoid modifying array inplace
    array = array.copy()

    # Iterate through all but last element in array
    for i in range(len(array)-1):

        # Find index of min value element out of array, starting after current
        # element
        # Initialise min val to last element in (sub-)array
        min_val = array[i+1:][-1]
        min_index = len(array[i+1:]) - 1
        for index, val in enumerate(array[i+1:]):
            if val < min_val:
                min_val = val
                min_index = index
        # Convert to index in array (not sliced)
        min_index = i + 1 + min_index

        # Swap current element with min element, if min element is smaller
        if array[i] > min_val:
            # Perform swap
            array[min_index] = array[i]
            array[i] = min_val

    if ascending:
        return array
    else:
        # Reverse array for descending order sort
        return array[::-1]