Add bucket sort in Python

pull/1179/head
Francesco Franco 2023-04-19 09:35:38 +02:00 committed by GitHub
parent af47764be0
commit e3b24861e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 51 additions and 0 deletions

51
bucket_sort.py 100644
View File

@ -0,0 +1,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)