def sort(_list, bucket_size=5):
    """
    bucket sort algorithm
    
    :param _list: list of values to sort
    :param bucket_size: Size of the bucket
    :return: sorted values
    """
    string = False

    if len(_list) == 0:
        # print("You don\'t have any elements in array!")
        raise ValueError("Array can not be empty.")

    elif all(isinstance(element, str) for element in _list):
        string = True
        _list = [ord(element) for element in _list]

    min_value = _list[0]
    max_value = _list[0]

    # For finding minimum and maximum values
    for i in range(0, len(_list)):
        if _list[i] < min_value:
            min_value = _list[i]
        elif _list[i] > max_value:
            max_value = _list[i]

    # Initialize buckets
    bucket_count = math.floor((max_value - min_value) / bucket_size) + 1
    buckets = []
    for i in range(0, int(bucket_count)):
        buckets.append([])

    # For putting values in buckets
    for i in range(0, len(_list)):
        # TODO: floor expects floats but could be receiving int or slice
        buckets[math.floor(float(
            (_list[i] - min_value) / bucket_size))].append(_list[i])

    # Sort buckets and place back into input array
    sorted_array = []
    for i in range(0, len(buckets)):
        insertion_sort.sort(buckets[i])
        for j in range(0, len(buckets[i])):
            sorted_array.append(buckets[i][j])

    if string:
        return [chr(element) for element in sorted_array]
    else:
        return sorted_array
Example #2
0
def sort(myList, bucketSize=5):
    from pygorithm.sorting import insertion_sort
    import math

    string = False

    if (len(myList) == 0):
        print('You don\'t have any elements in array!')
    elif all(isinstance(element, str) for element in myList):
        string = True
        myList = [ord(element) for element in myList]

    minValue = myList[0]
    maxValue = myList[0]

    # For finding minimum and maximum values
    for i in range(0, len(myList)):
        if myList[i] < minValue:
            minValue = myList[i]
        elif myList[i] > maxValue:
            maxValue = myList[i]

    # Initialize buckets
    bucketCount = math.floor((maxValue - minValue) / bucketSize) + 1
    buckets = []
    for i in range(0, int(bucketCount)):
        buckets.append([])

    # For putting values in buckets
    for i in range(0, len(myList)):
        buckets[int(math.floor(
            (myList[i] - minValue) / bucketSize))].append(myList[i])

    # Sort buckets and place back into input array
    sortedArray = []
    for i in range(0, len(buckets)):
        insertion_sort.sort(buckets[i])
        for j in range(0, len(buckets[i])):
            sortedArray.append(buckets[i][j])

    if string:
        return [chr(element) for element in sortedArray]
    else:
        return sortedArray
Example #3
0
def sort(myList, bucketSize = 5):
    from pygorithm.sorting import insertion_sort
    import math

    string = False

    if(len(myList) == 0):
        print('You don\'t have any elements in array!')
    elif all(isinstance(element, str) for element in myList):
        string = True
        myList = [ord(element) for element in myList]

    minValue = myList[0]
    maxValue = myList[0]

    # For finding minimum and maximum values
    for i in range(0, len(myList)):
        if myList[i] < minValue:
            minValue = myList[i]
        elif myList[i] > maxValue:
            maxValue = myList[i]

    # Initialize buckets
    bucketCount = math.floor((maxValue - minValue) / bucketSize) + 1
    buckets = []
    for i in range(0, bucketCount):
        buckets.append([])

    # For putting values in buckets
    for i in range(0, len(myList)):
        buckets[math.floor((myList[i] - minValue) / bucketSize)].append(myList[i])

    # Sort buckets and place back into input array
    sortedArray = []
    for i in range(0, len(buckets)):
        insertion_sort.sort(buckets[i])
        for j in range(0, len(buckets[i])):
            sortedArray.append(buckets[i][j])

    if string:
        return [chr(element) for element in sortedArray]
    else:
        return sortedArray
Example #4
0
def sort(_list):
    min_n = max_n = _list[0]
    for n in _list:
        if n > max_n:
            max_n = n
        if n < min_n:
            min_n = n
    bucket_n = int(math.ceil((max_n - min_n)/bucket_size))+1
    bucket = []
    for i in range(bucket_n):
        bucket.append([])

    for n in _list:
        bucket[int(math.floor((n-min_n)/bucket_size))].append(n)

    result = []
    for n in bucket:
        insertion_sort.sort(n)
        result += n

    return result
    def test_insertion_sort(self):
        self.result = insertion_sort.sort(self.array)
        self.assertEqual(self.result, self.sorted_array)

        self.alphaResult = insertion_sort.sort(self.alphaArray)
        self.assertEqual(self.alphaResult, self.sorted_alpha_array)
Example #6
0
 def sort(arr):
     return insertion_sort.sort(arr)