"""
Bucketsort implementation using linked lists

Copyright 2024, University of Freiburg.

Philipp Schneider <philipp.schneider@cs.uni-freiburg.de>
Marc Fuchs <marc.fuchs@cs.uni-freiburg.de>
"""

from random import randint
import statistics
import time
import Queuee

from ListElement import ListElement


def bucket_sort(array, k, key=lambda x: x):
    '''
    Implements the bucket sort algorithm to sort
        data elements using a key function to
        assign sorting keys to data elements

    Args:
        array: array of data elements
        k: largest key
        key: a function mapping data elements to values
        in range(k+1) (idendity function as default)

    >>> array = []
    >>> bucket_sort(array, 10)
    >>> str(array)
    '[]'
    >>> array = [10-i for i in range(10)]
    >>> bucket_sort(array, 10)
    >>> str(array)
    '[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]'
    '''

    bucket = [Queuee.Queue() for _ in range(k + 1)]

    # put dataelements into according buckets
    for i in range(len(array)):
        bucket[key(array[i])].enqueue(ListElement(array[i]))

    # fill keys from buckets back into array
    i = 0
    for j in range(k+1):
        while not bucket[j].is_empty():
            array[i] = bucket[j].dequeue().get_key()
            i += 1


def is_sorted(lst):
    """
        Method that checks whether or not a list is sorted

        Args:
            lst: the list that will be checked
        Returns:
        bool:    True if the list is sorted, false otherwise.
        """
    return all(lst[i] <= lst[i + 1] for i in range(len(lst) - 1))


def bucket_sort_performance(size=2*10**4, initial_k=2*10**4,
                            k_increment=2*10**4,
                            max_k=(12*(10**5))+1,
                            repetitions=10):
    """
    Method that outputs array size and elapsed time for sorting.

    Args:
        size:           number of elements in the arrays
        initial_k:      initial value for the largest value
        k_increment:    increment for the largest value
        max_k:          upperbound for the largest number
        repetitions:    number of repetitions per k
    Returns:
        results:        list with the running times of all executions
    Raises:
        Exception:      when quicksort did not sort the array correctly
    """
    results = []
    for k in range(initial_k, max_k, k_increment):
        for _ in range(repetitions):
            execution_results = []
            array = [randint(0, k) for _ in range(size)]

            start_time = time.time()
            bucket_sort(array, k)
            run_time = (time.time() - start_time) * 1000

            if not is_sorted(array):
                raise Exception("list not sorted successfully")
            execution_results.append(run_time)
        results.append(statistics.mean(execution_results))
        print("{}\t{}\t{:.1f}".format(size, k, results[-1]))
    return results


if __name__ == "__main__":
    bucket_sort_performance()
