Lecture 9
Lecture 9
September 9, 2024
Bucket Sort
Assumptions
Assumptions
Input is uniformly independently distributed.
Assumptions
Input is uniformly independently distributed.
Each element belongs to (0,1).
Assumptions
Input is uniformly independently distributed.
Each element belongs to (0,1).
Bucket Sort
Assumptions
Input is uniformly independently distributed.
Each element belongs to (0,1).
Bucket Sort
It divides the [0,1] into n-equal sized subintervals or buckets.
Assumptions
Input is uniformly independently distributed.
Each element belongs to (0,1).
Bucket Sort
It divides the [0,1] into n-equal sized subintervals or buckets.
After that it distributes the n input numbers into the buckets.
Assumptions
Input is uniformly independently distributed.
Each element belongs to (0,1).
Bucket Sort
It divides the [0,1] into n-equal sized subintervals or buckets.
After that it distributes the n input numbers into the buckets.
Call the insertion sort on each bucket.
Assumptions
Input is uniformly independently distributed.
Each element belongs to (0,1).
Bucket Sort
It divides the [0,1] into n-equal sized subintervals or buckets.
After that it distributes the n input numbers into the buckets.
Call the insertion sort on each bucket.
Concatenate each bucket.
n−1
X
T (n) = Θ(n) + O(ni2 )
i=0
n−1
X
T (n) = Θ(n) + O(ni2 )
i=0
n−1
X
T (n) = Θ(n) + O(ni2 )
i=0
n−1
X
T (n) = Θ(n) + O(ni2 )
i=0
n−1
X
E [T (n)] = Θ(n) + E [O(ni2 )] {By linearity of expectation}
i=0
n−1
X
T (n) = Θ(n) + O(ni2 )
i=0
n−1
X
E [T (n)] = Θ(n) + E [O(ni2 )] {By linearity of expectation}
i=0
n−1
X
E [T (n)] = Θ(n) + O(E [ni2 ])
i=0
n
X
ni = Xi,j
j=1
n
X
ni = Xi,j
j=1
P(Xi,j = 1) = 1/n
" n
!2 #
X
E [ni2 ] =E Xi,j
j=1
" n
!2 #
X
E [ni2 ] =E Xi,j
j=1
n
" #
X X X
2
=E Xi,j + Xi,j Xi,k
j=1 1≤j≤n 1≤k≤n
j̸=k
" n
!2 #
X
E [ni2 ] =E Xi,j
j=1
n
" #
X X X
2
=E Xi,j + Xi,j Xi,k
j=1 1≤j≤n 1≤k≤n
j̸=k
n
X X X
2
= E [Xi,j ]+ E [Xi,j Xi,k ]
j=1 1≤j≤n 1≤k≤n
j̸=k
" n
!2 #
X
E [ni2 ] =E Xi,j
j=1
n
" #
X X X
2
=E Xi,j + Xi,j Xi,k
j=1 1≤j≤n 1≤k≤n
j̸=k
n
X X X
2
= E [Xi,j ]+ E [Xi,j Xi,k ]
j=1 1≤j≤n 1≤k≤n
j̸=k
2 1
E [Xi,j ] = 12 . = 1/n which is also equal toE [Xi,j ]
n
Prof. Prateek Vishnoi Bucket Sort
Continued...
1 1 1
E [ni2 ] = n. + 2.n C2 . .
n n n
1 1
= 1 + n(n − 1). 2 = 2 −
n n
1 1 1
E [ni2 ] = n. + 2.n C2 . .
n n n
1 1
= 1 + n(n − 1). 2 = 2 −
n n
n−1
X
E [T (n)] = Θ(n) + O(E [ni2 ])
i=0
1 1 1
E [ni2 ] = n. + 2.n C2 . .
n n n
1 1
= 1 + n(n − 1). 2 = 2 −
n n
n−1
X
E [T (n)] = Θ(n) + O(E [ni2 ])
i=0
1
E [T (n)] = Θ(n) + n.(2 − ) = Θ(n)
n