# L-16 MCS 275 Wed 15 Feb 2017 : selectsort.py
"""
In selection sort we select the minimum of
the list and put it in first place.
The minimum of the rest of the list is
put in second place, etc...
"""

from random import randint

def select_inplace(data):
    """
    Sorts the data in increasing order.
    This is the in place select sort.
    """
    for k in range(len(data)-1):
        sel = data[k:len(data)]
        minsel = min(sel)
        idx = k + sel.index(minsel)
        if idx != k:
            data[idx] = data[k]
            data[k] = minsel

def iterative_select(data):
    """
    Returns a list with the data
    sorted in increasing order.
    """
    result = []
    while len(data) > 0:
        mindata = min(data)
        result.append(mindata)
        data.pop(data.index(mindata))
    return result

def recursive_select(data):
    """
    Returns a list with the data
    sorted in increasing order.
    """
    if len(data) <= 1:
        return data
    else:
        mindata = min(data)
        data.pop(data.index(mindata))
        return [mindata] + recursive_select(data)

def main():
    """
    Calls select sort on a list of
    randomly generated numbers.
    """
    low = int(input('Give lower bound : '))
    upp = int(input('Give lower bound : '))
    size = int(input('How many numbers ? '))
    data = [randint(low, upp) for _ in range(size)]
    import copy
    backup1 = copy.deepcopy(data)
    backup2 = copy.deepcopy(data)
    print('data =', data)
    select_inplace(data)
    print('after inplace sort :  ', data)
    srt1 = recursive_select(backup1)
    print('after recursive sort :', srt1)
    srt2 = iterative_select(backup2)
    print('after iterative sort :', srt2)

if __name__ == "__main__":
    main()
