⬅ Previous Topic
Counting SortNext Topic ⮕
Shell Sort⬅ Previous Topic
Counting SortNext Topic ⮕
Shell Sortdef bucket_sort(arr):
n = len(arr)
if n == 0:
return arr
# Create n empty buckets
buckets = [[] for _ in range(n)]
# Put array elements in different buckets
for num in arr:
index = int(num * n) if num < 1 else n - 1
buckets[index].append(num)
# Sort individual buckets and concatenate
sorted_arr = []
for bucket in buckets:
sorted_arr.extend(sorted(bucket))
return sorted_arr
if __name__ == '__main__':
arr = [0.42, 0.32, 0.23, 0.52, 0.25, 0.47, 0.51]
sorted_arr = bucket_sort(arr)
print("Sorted array is:", sorted_arr)
⬅ Previous Topic
Counting SortNext Topic ⮕
Shell SortYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.