Bucket sort runs in linear time on the average. It assumes that the input is generated by a random process that distributes elements uniformly over the interval [0, 1).

The idea of Bucket sort is to divide the interval [0, 1) into n equal-sized subintervals, or buckets, and then distribute the n input numbers into the buckets. Since the inputs are uniformly distributed over (0, 1), we don't expect many numbers to fall into each bucket. To produce the output, simply sort the numbers in each bucket and then go through the bucket in order, listing the elements in each.

The code assumes that input is in n-element array
A and each element in
A satisfies 0 ≤ *A*[*i*] ≤
1. We also need an auxiliary array B[0
. . *n *-1] for linked-lists (buckets).

BUCKET_SORT (A)

n← length [A]- For
i= 1 tondo- Insert
A[i] into listB[nA[i]]- For
i= 0 ton-1 do- Sort list
Bwith Insertion sort- Concatenate the lists
B[0],B[1], . .B[n-1] together in order.

Given input array A[1..10]. The array
B[0..9] of sorted lists or buckets
after line 5. Bucket i holds values in the interval [*i*/10, (*i
*+1)/10]. The sorted output consists of a concatenation
in order of the lists first B[0]
then B[1] then
B[2] ... and the last one is
B[9].

All lines except line 5 take O(*n*) time in the worst case. We
can see inspection that total time to examine all buckets in line 5 is
O(*n*-1)
i.e., O(*n*).

The only interesting part of the analysis is the time taken by Insertion sort
in line 5. Let *n _{i}* be the random variable denoting the number
of elements in the bucket

*E*[O(^{2}*n _{i}*)]
= O(

Therefore, the total expected time to sort all elements in all buckets is

^{
n-1}∑_{i=0} *O*(*E*[^{2}*n _{i}*])
=

In order to evaluate this summation, we must determine the distribution of each random variable

nWe have

nelements and n buckets. The probability that a given element falls in a bucketB[i] is 1/ni.e., Probability =p= 1/n. (Note that this problem is the same as that of "Balls-and-Bin" problem).Therefore, the probability follows the binomial distribution, which has

mean: E[

n] =_{i}np= 1

variance: Var[n] =_{i}np(1-p) = 1- 1/nFor any random variable, we have

E[^{2}n] = Var[_{i}n] +_{i}E^{2}[n]_{i}

= 1 - 1/n+ 1^{2}

= 2 - 1/n

= (1)

Putting this value in equation A above, (do some tweaking) and we have
a expected time for INSERTION_SORT, O(*n*).

Now back to our original problem

In the above Bucket sort algorithm, we observe

T(n) = [time to insert

nelements in arrayA] + [time to go through auxiliary arrayB[0 . .n-1] * (Sort by INSERTION_SORT)

= O(n) + (n-1) (n)

= O(n)

Therefore, the entire Bucket sort algorithm runs in linear expected time.