1 #-*- coding: UTF-8 -*- 2 import numpy as np 3 from QuickSort import QuickSort 4 5 def BucketSort(a, n): 6 barrel = {} 7 for i in xrange(0,n): 8 barrel.setdefault(i, []) 9 min = np.min(a) 10 max = np.max(a) 11 for x in a: 12 for i in xrange(0,n-1): 13 if x >= min +i* (max - min)/n and x < min +(i +1) * (max - min)/n: 14 barrel[i].append(x) 15 elif i == n-2 and x >= min +(i +1) * (max - min)/n: 16 barrel[i+1].append(x) 17 k = 0 18 for i in xrange(0,n): 19 if len(barrel[i]) != 0: 20 arr = np.array(barrel[i]) 21 QuickSort(arr, 0, len(barrel[i]) -1) 22 for x in arr: 23 a[k] = x 24 k += 1 25 26 27 28 if __name__ == '__main__': 29 a = np.random.randint(0, 100, size = 10) 30 print "Before sorting..." 31 print "---------------------------------------------------------------" 32 print a 33 print "---------------------------------------------------------------" 34 BucketSort(a, 10) 35 print "After sorting..." 36 print "---------------------------------------------------------------" 37 print a 38 print "---------------------------------------------------------------"