-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket_sort.py
51 lines (28 loc) · 1.06 KB
/
bucket_sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def insertion_sort(bucket):
for i in range(1,len(bucket)):
val=bucket[i]
j=i-1
while j >= 0 and bucket[j] > val:
bucket[j+1]=bucket[j]
j-=1
bucket[j+1] = val
def bucket_sort(input_list):
max_value=max(input_list)
size=max_value / len(input_list)
buckets_list=[]
for x in range(len(input_list)):
buckets_list.append([])
for i in range(len(input_list)):
j=int(input_list[i] /size)
if j != len(input_list):
buckets_list[j].append(input_list[i])
else:
buckets_list[len(input_list)-1].append(input_list[i])
for z in range(len(input_list)):
insertion_sort(buckets_list[z])
final_output=[]
for x in range(len(input_list)):
final_output += buckets_list[x]
print(final_output)
nums=[12,564,23,54,54,34,8,9,64,23,3,2,4,6]
bucket_sort(nums)