What is the time complexity of the bucket sort algorithm?

Algorithm Design Questions Medium



49 Short 51 Medium 39 Long Answer Questions Question Index

What is the time complexity of the bucket sort algorithm?

The time complexity of the bucket sort algorithm is O(n + k), where n is the number of elements to be sorted and k is the number of buckets.

In bucket sort, the input elements are distributed into a number of buckets based on their values. Each bucket is then sorted individually, either using another sorting algorithm or recursively applying bucket sort. Finally, the sorted elements from all the buckets are concatenated to obtain the sorted output.

The time complexity of distributing the elements into buckets is O(n), as each element needs to be compared and placed into the appropriate bucket. The time complexity of sorting each bucket depends on the sorting algorithm used, which can vary from O(n log n) for efficient sorting algorithms like quicksort or mergesort, to O(n^2) for less efficient algorithms like insertion sort.

Since the number of buckets, k, is typically much smaller than the number of elements, n, the time complexity of sorting the buckets is usually dominated by the time complexity of distributing the elements into buckets. Therefore, the overall time complexity of bucket sort is O(n + k).