|
1 | | -# Author: OMKAR PATHAK |
2 | | -# Created On: 31st July 2017 |
| 1 | +""" |
| 2 | +Author: OMKAR PATHAK |
| 3 | +Created On: 31st July 2017 |
3 | 4 |
|
4 | | -# Best O(n); Average O(n); Worst O(n) |
| 5 | + - Best O(n) |
| 6 | + - Average O(n) |
| 7 | + - Worst O(n) |
| 8 | +""" |
| 9 | +import math |
| 10 | +from pygorithm.sorting import insertion_sort |
| 11 | +import inspect |
5 | 12 |
|
6 | | -# bucket sort algorithm |
7 | | -def sort(List, bucketSize = 5): |
8 | | - from pygorithm.sorting import insertion_sort |
9 | | - import math |
10 | 13 |
|
| 14 | +def sort(_list, bucket_size=5): |
| 15 | + """ |
| 16 | + bucket sort algorithm |
| 17 | + |
| 18 | + :param _list: list of values to sort |
| 19 | + :param bucket_size: Size of the bucket |
| 20 | + :return: sorted values |
| 21 | + """ |
11 | 22 | string = False |
12 | 23 |
|
13 | | - if(len(List) == 0): |
14 | | - print('You don\'t have any elements in array!') |
15 | | - elif all(isinstance(element, str) for element in List): |
| 24 | + if len(_list) == 0: |
| 25 | + # print("You don\'t have any elements in array!") |
| 26 | + raise ValueError("Array can not be empty.") |
| 27 | + |
| 28 | + elif all(isinstance(element, str) for element in _list): |
16 | 29 | string = True |
17 | | - List = [ord(element) for element in List] |
| 30 | + _list = [ord(element) for element in _list] |
18 | 31 |
|
19 | | - minValue = List[0] |
20 | | - maxValue = List[0] |
| 32 | + min_value = _list[0] |
| 33 | + max_value = _list[0] |
21 | 34 |
|
22 | 35 | # For finding minimum and maximum values |
23 | | - for i in range(0, len(List)): |
24 | | - if List[i] < minValue: |
25 | | - minValue = List[i] |
26 | | - elif List[i] > maxValue: |
27 | | - maxValue = List[i] |
| 36 | + for i in range(0, len(_list)): |
| 37 | + if _list[i] < min_value: |
| 38 | + min_value = _list[i] |
| 39 | + elif _list[i] > max_value: |
| 40 | + max_value = _list[i] |
28 | 41 |
|
29 | 42 | # Initialize buckets |
30 | | - bucketCount = math.floor((maxValue - minValue) / bucketSize) + 1 |
| 43 | + bucket_count = math.floor((max_value - min_value) / bucket_size) + 1 |
31 | 44 | buckets = [] |
32 | | - for i in range(0, bucketCount): |
| 45 | + for i in range(0, int(bucket_count)): |
33 | 46 | buckets.append([]) |
34 | 47 |
|
35 | 48 | # For putting values in buckets |
36 | | - for i in range(0, len(List)): |
37 | | - buckets[math.floor((List[i] - minValue) / bucketSize)].append(List[i]) |
| 49 | + for i in range(0, len(_list)): |
| 50 | + # TODO: floor expects floats but could be receiving int or slice |
| 51 | + buckets[math.floor(float((_list[i] - min_value) / bucket_size))].append(_list[i]) |
38 | 52 |
|
39 | 53 | # Sort buckets and place back into input array |
40 | | - sortedArray = [] |
| 54 | + sorted_array = [] |
41 | 55 | for i in range(0, len(buckets)): |
42 | 56 | insertion_sort.sort(buckets[i]) |
43 | 57 | for j in range(0, len(buckets[i])): |
44 | | - sortedArray.append(buckets[i][j]) |
| 58 | + sorted_array.append(buckets[i][j]) |
45 | 59 |
|
46 | 60 | if string: |
47 | | - return [chr(element) for element in sortedArray] |
| 61 | + return [chr(element) for element in sorted_array] |
48 | 62 | else: |
49 | | - return sortedArray |
| 63 | + return sorted_array |
50 | 64 |
|
51 | | -# time complexities |
| 65 | + |
| 66 | +# TODO: Are these necessary? |
52 | 67 | def time_complexities(): |
53 | | - return '''Best Case: O(n), Average Case: O(n), Worst Case: O(n)''' |
| 68 | + """ |
| 69 | + Return information on functions |
| 70 | + time complexity |
| 71 | + :return: string |
| 72 | + """ |
| 73 | + return "Best Case: O(n), Average Case: O(n), Worst Case: O(n)" |
| 74 | + |
54 | 75 |
|
55 | | -# easily retrieve the source code of the sort function |
56 | 76 | def get_code(): |
57 | | - import inspect |
| 77 | + """ |
| 78 | + easily retrieve the source code |
| 79 | + of the sort function |
| 80 | +
|
| 81 | + :return: source code |
| 82 | + """ |
58 | 83 | return inspect.getsource(sort) |
0 commit comments