(myList, bucketSize=DEFAULT_BUCKET_SIZE)
| 20 | DEFAULT_BUCKET_SIZE = 5 |
| 21 | |
| 22 | def bucketSort(myList, bucketSize=DEFAULT_BUCKET_SIZE): |
| 23 | if(len(myList) == 0): |
| 24 | print('You don\'t have any elements in array!') |
| 25 | |
| 26 | minValue = myList[0] |
| 27 | maxValue = myList[0] |
| 28 | |
| 29 | # For finding minimum and maximum values |
| 30 | for i in range(0, len(myList)): |
| 31 | if myList[i] < minValue: |
| 32 | minValue = myList[i] |
| 33 | elif myList[i] > maxValue: |
| 34 | maxValue = myList[i] |
| 35 | |
| 36 | # Initialize buckets |
| 37 | bucketCount = math.floor((maxValue - minValue) / bucketSize) + 1 |
| 38 | buckets = [] |
| 39 | for i in range(0, bucketCount): |
| 40 | buckets.append([]) |
| 41 | |
| 42 | # For putting values in buckets |
| 43 | for i in range(0, len(myList)): |
| 44 | buckets[math.floor((myList[i] - minValue) / bucketSize)].append(myList[i]) |
| 45 | |
| 46 | # Sort buckets and place back into input array |
| 47 | sortedArray = [] |
| 48 | for i in range(0, len(buckets)): |
| 49 | insertion_sort(buckets[i]) |
| 50 | for j in range(0, len(buckets[i])): |
| 51 | sortedArray.append(buckets[i][j]) |
| 52 | |
| 53 | return sortedArray |
| 54 | |
| 55 | if __name__ == '__main__': |
| 56 | sortedArray = bucketSort([12, 23, 4, 5, 3, 2, 12, 81, 56, 95]) |
no test coverage detected