MCPcopy Index your code
hub / github.com/subbarayudu-j/TheAlgorithms-Python / bucketSort

Function bucketSort

sorts/bucket_sort.py:22–53  ·  view source on GitHub ↗
(myList, bucketSize=DEFAULT_BUCKET_SIZE)

Source from the content-addressed store, hash-verified

20DEFAULT_BUCKET_SIZE = 5
21
22def 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
55if __name__ == '__main__':
56 sortedArray = bucketSort([12, 23, 4, 5, 3, 2, 12, 81, 56, 95])

Callers 1

bucket_sort.pyFile · 0.85

Calls 1

insertion_sortFunction · 0.90

Tested by

no test coverage detected