Javatpoint Logo
Javatpoint Logo

Difference Between Counting Sort and Bucket Sort

Counting Sort Algorithm:

Counting sort is a sorting algorithm that deals with the scope of the input values. The counting sort algorithm is an integer sorting algorithm.Counting sort is to some degree not the same as other arranging strategies, as it is a linear sorting algorithm.

It counts the number of occurrences of every element and determines their positions directly. This implies it runs in learn time O(N), while the best examination-based sorting algorithms have the complexity of O(N log N) (where N is the number of elements in an array).

Basic Concept:

  • Sorting is done according to keys.
  • Counting the elements having distinct key values.


Let's consider an array A of length N which we are going to sort using counting sort, and an array count of length K where K is max_ele-min_ele + 1.

Array A = {2,1,1,0,2,5,4,0,2,8,7,7,9,2,0,1,9}


  • Utilize a count array to store the counts for all unique numbers in input array A.
  • Alter the count array such that the components at each index store the sum of past counts.
  • Presently emphasize over A and yield every number to its related position in the output array.

Iteration steps:

Difference Between Counting Sort and Bucket Sort

Difference Between Counting Sort and Bucket Sort


For algorithm execution, we take a helper array that stores the count of the events of every component in the array lying between 0 to N - 1, then the array is utilized to deliver the arranged adaptation of the exhibit utilizing math activities.

Here we are utilizing an additional array which expands the space complexity. This makes it a bit more expensive than other algorithms, as it works just with ranges and is consequently just reasonable where the component values' reach is near the size of the array and the reach = max_element - min_element + 1.


The code listed below is executed and counters the negative number. Here, we base on the minimum element andcount stored at 0 to keep this will be counted.

Difference Between Counting Sort and Bucket Sort

Space complexity: O(N)

Modifying count array: O(K)

Output integers to output array: In the worst case, O(N).

Overall time complexity: O(kn)

Practically, it becomes O(N).

Bucket Sort Algorithm:

Bucket sort is a beginning algorithm mainly utilized when we have data consistently conveyed over a range. As the name recommends, in bucket sort we have a few groups called buckets, each containing determined components of the input array. Each bucket is then arranged by either recursively applying a similar bucket algorithm or appropriate sorting algorithms.

Essential steps for bucket sorting:

  • Partition the range into a fixed number of groups.
  • Iterate over the components whenever you have placed the components into the suitable group.
  • Sort the buckets exclusively utilizing some sorting algorithm.
  • Then, link the groups together.


Let's consider an example.

A = {10, 8, 20, 7, 16, 18, 12, 1, 23, 11} and Here N = no of elements = 9

We can observe from the given array the elements are in a range [0,25). The buckets we can use can be in the following ranges:

Bucket 1 - [0-5]

Bucket 2 - [5-10]

Bucket 3 - [10-15]

Bucket 4 - [15-20]

Bucket 5 - [20-25]

We can see five buckets with a distributed range.

Now the elements are pushed into buckets based on their element range.

Bucket 1 - [1]

Bucket 2 - [7,8]

Bucket 3 - [10,11,12]

Bucket 4 - [16,18,20]

Bucket 5 - [23]

As our final step, we just concatenate these buckets to get the desired output array.

Difference Between Counting Sort and Bucket Sort

Code Implementation: Java


The best case in terms of complexity - is the scenario when an array has already been sorted and elements each have an even partition, which results in a uniform distribution across all buckets. It would go better if we also sorted the elements in the bucket. Since the individual buckets aren't sorted, we can go beyond and further refine our complexity with insertion sort. This will make our overall complexity linear, i.e., O(N + k), where the first term is used for distribution among buckets while the second one signifies input to be inserted into the proper place in the bucket via the use of insertion sort within each binary heap or bucket. For the best case, the time complexity of bucket sort is O(N + k).

Average-case complexity - occurs in the absence of an appropriate ascending or descending sorted order. Even if the elements are uniformly distributed, bucket sort is still linear in time. The order of complexity for the average case-time complexity is N + K.

Worst-case complexity - happens when elements are nearer. It stands for a minor deviation, and because of this property, they are put into the same bucket, from which some buckets contain more items as opposed to others. The situation will develop further, though it is already complicated enough if it involves elements listed in reverse order. Bucket sort is a time-complex O(n2).

Counting Sort vs Bucket Sort

Counting Sort Bucket Sort
Counts the number of occurrences of every element and determines their positions directly. Divides the array into buckets, sorts thebuckets separately, and then merges or concatenates the sorted buckets.
The counting sort algorithm is an integer sorting algorithm. Bucket sort is a distributed sorting algorithm.
Requires additional memory for the count array. Requires the additional memory for the buckets.
The average time complexity for counting sort is O(n+k)
n-no of elements
k-range of input values.
The average time complexity for bucket sort is O(n+k)
n-no of elements
k-no of buckets
Limited to sorting integers with a discrete range of keys. Can handle various types of data.


To summarize, counting sort and bucket sort are linear sort algorithms that make use of some features of input data to obtain efficiency. Counting Sort, with a time complexity of O (KN), where K is the range of input values, works well for small ranges relative to N. Though its count array results in a space complexity of O(N), another aspect makes Counting Sort stable and optimal when used for small values.

However, bucket sort is aimed at the distribution of elements in buckets and sorting them independently, rather than using another algorithm like insertion inside each bucket. It is appropriate for uniformly distributed data, with an average time complexity of O(n + k), but the worst case occurs when elements are grouped and a respective time complexity appears to be O(n^2). Bucket Sort has benefits with reduced comparisons and faster processing even for equably distributed elements, but may lead to efficiency degradation if datasets are larger.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA