Bucket Sort Algorithm

SHIVANGI PANDEY
5 min readOct 9, 2020

--

BUCKET SORT ALGORITHM

* We will learn how bucket sort works,

  • Bucket Sort is a sorting technique .

* The complexity of the Bucket Sort Technique

# Time Complexity: O(n + k) for best case and O(n²) for the worst case.

#Space Complexity: O(n.k) for worst case

* WORKING:-

  • It sorts the elements by first dividing the elements into several groups called buckets.
  • The elements inside each bucket are sorted using any of the suitable sorting algorithms or recursively calling the same algorithm.
  • Many of the buckets are created.
  • Each bucket is filled with a specific range of elements.
  • The elements inside the bucket are sorted using any other algorithm.
  • Finally, the elements of the bucket are gathered to get the sorted array.
  • The process of bucket sort can be understood as a scatter-gather approach (i.e the elements are first scattered into buckets then the elements of buckets are sorted. Finally, the elements are gathered in order)
Working of Bucket Sort

Now we see,

* How Bucket Sort Works?

  1. Suppose, the input array is
  • Create an array of size 10.
  • Each slot of this array is used as a bucket for storing elements.

2. Insert elements into the buckets from the array.

  • The elements are inserted according to the range of the bucket.
  • In our example code, we have buckets each of ranges from 0 to 1, 1 to 2, 2 to 3,…… (n-1) to n.
  • Suppose, an input element is .23 is taken. It is multiplied by size = 10 (i.e.23*10=2.3).
  • Then, it is converted into an integer (i.e 2.3≈2). Finally, .23 is inserted into bucket-2.
  • Similarly, .25 is also inserted into the same bucket.
  • Each time he floor value of the floating point number is taken.
  • If we take integer numbers as input, we have to divide it by the interval (10 here) to get the floor value.Similarly, other elements are inserted into their respective buckets.

3.The elements of each bucket are sorted using any of the stable sorting algorithms.

  • Here, we have used Quick sort (inbuilt function).

4. The elements from each bucket are gathered.

  • It is done by iterating through the bucket and inserting an individual element into the original array in each cycle.
  • The element from the bucket is erased once it is copied into the original array.

ALGORITHM:-

bucketSort()  create N buckets each of which can hold a range of values
for all the buckets
initialize each bucket with 0 values
for all the buckets
Then put elements into buckets matching the range
for all the buckets
sort elements in each bucket gather elements from each bucketend bucketSort

EXPLANATION USING PYTHON EXAMPLE:-

# Bucket Sort in Python


def bucketSort(array):
bucket = []

# Create empty buckets
for i in range(len(array)):
bucket.append([])

# Insert elements into their respective buckets
for j in array:
index_b = int(10 * j)
bucket[index_b].append(j)

# Sort the elements of each bucket
for i in range(len(array)):
bucket[i] = sorted(bucket[i])

# Get the sorted elements
k = 0
for i in range(len(array)):
for j in range(len(bucket[i])):
array[k] = bucket[i][j]
k += 1
return array


array = [.42, .32, .33, .52, .37, .47, .51]
print("Sorted Array in descending order is")
print(bucketSort(array))

Complexity:-

  1. Worst Case Complexity:
  • When there are elements of close range in the array, they are likely to be placed in the same bucket.
  • This may result in some buckets having more number of elements than others.
  • It makes the complexity depend on the sorting algorithm used to sort the elements of the bucket.
  • The complexity becomes even worse when the elements are in reverse order. If insertion sort is used to sort elements of the bucket, then the time complexity becomes O(n2).

2. Best Case Complexity:

  • It occurs when the elements are uniformly distributed in the buckets with a nearly equal number of elements in each bucket.
  • The complexity becomes even better if the elements inside the buckets are already sorted.
  • IMPORTANT POINT:- If insertion sort is used to sort elements of a bucket then the overall complexity in the best case will be linear i.e. O(n+k). O(n) is the complexity for making the buckets and O(k) is the complexity for sorting the elements of the bucket using algorithms having linear time complexity at the best case.

3. Average Case Complexity:

  • It occurs when the elements are distributed randomly in the array. Even if the elements are not distributed uniformly, bucket sort runs in linear time.
  • It holds true until the sum of the squares of the bucket sizes is linear in the total number of elements.
  • Average Case Complexity- o(n)

APPLICATIONS:-

  • Bucket sort is mainly useful when input is uniformly distributed over a range. For example, consider the following problem.
  • Suppose we have to sort a large set of floating point numbers which are in range from 0.0 to 1.0 and are uniformly distributed across the range. How do we sort the numbers efficiently?
  • A simple way is to apply a comparison based sorting algorithm (Merge Sort, Heap Sort, Quick-Sort …. etc) is Ω(n Log n), i.e., they cannot do better than (nLogn) .
    Can we sort the array in linear time? Here keys are floating point numbers.
    The idea is to use bucket sort Algorithm only.
GRAPH

THANK YOU :)

--

--

SHIVANGI PANDEY
SHIVANGI PANDEY

Written by SHIVANGI PANDEY

No one is born as a writer .You must become a writer because you never stop learning:-).

No responses yet