CS 251 – Design and Analysis of Algorithms
Instructor: Dr. Zuhair Zafar
Lecture # 15: Bucket Sort
1
Types of Sorting Algorithms
• Non-recursive/Incremental comparison sorting
• Selection sort
• Bubble sort
• Insertion sort
• Recursive comparison sorting
• Merge sort
• Quick sort
• Heap sort
• Non-comparison linear sorting
• Counting sort
• Radix sort
2
• Bucket sort
Bucket Sort
• Bucket Sort is a sorting technique that 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.
• Several buckets are created. Each bucket is filled with a specific
range of elements.
• The elements inside the bucket are sorted using any other
algorithm such as, insertion sorting algorithm.
• Finally, the elements of the bucket are gathered to get the sorted 3
array.
Bucket Sort
• Assumption:
• Input numbers are uniformly distributed in [0,1).
• Suppose input size is n.
• Idea:
• Divide [0,1) into n equal-sized buckets (k= (n))
• Distribute the n input values into the buckets
• Sort each bucket (insertion sort as default).
• Go through the buckets in order, listing elements in each one
• Input: A[1 . . n], where 0 ≤ A[i] < 1 for all i
4
• Output: elements A[i] sorted
Bucket Sort
Step 1: Distribute into Buckets
.78 0
.17 1 .17 .12
.39 2 .26 .21 .23
.26 3 .39
.72 4 Multiply the keys with array size
.94 5
.21 6 .68
.12 7 .78 .72
.23 8
.68 9 .94 5
Bucket Sort
Step 1: Distribute into Buckets
.78 0
.17 1 .17
.12 .12
.17
.39 2 .26
.21 .21
.23 .23
.26
.26 3 .39
.72 4
Step 2: Sort within each Bucket
.94 5
.21 6 .68
.12 7 .78
.72 .72
.78
.23 8
.68 9 .94 6
Bucket Sort
Step 1: Distribute into Buckets
.78 0
.17 1 .12 .17
.39 2 .21 .23 .26
.26 3 .39
.72 4
Step 2: Sort within each Bucket
.94 5
.21 6 .68
.12 7 .72 .78
.23 8
.68 9 .94 7
Step 3: Concatenate all Buckets
.12 .17 .21 .23 .26 .39 .68 .72 .78 .94 /
Pseudocode of Bucket Sort
8
Worst Case Complexity of Bucket Sort
• 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 𝑂(𝑛2 )
9
Worst Case Example
Step 1: Distribute into Buckets
.79 0
.78 1
.77 2
.76 3
.75 4 Multiply the keys with array size
.74 5
.73 6
.72 7 .79 .78 .77 .76 .75 .74 .73 .72 .71 .70
.71 8
.70 9 10
Using Insertion Sort, the time complexity will be O(n2)
Best and Average Case Complexity
of Bucket Sort
• 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.
• 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) is the complexity of creating buckets and O(k) for sorting the
elements of the buckets.
Applications: Bucket sort is used when:
11
• input is uniformly distributed over a range.
• there are floating point values
Summary: Sorting Algorithms
Insertion Sort: Suitable only for small n ≤ 50 or nearly sorted inputs
Merge Sort: Guaranteed to be fast even in its worst case; stable
Quick Sort: Most useful general-purpose sorting for very little memory
requirement and fastest average time. (Choose the median of
three elements as pivot in practice)
Heap Sort: Requiring minimum memory and guaranteed to run fast;
average and maximum time both roughly more than the
average time of quick sort. Useful for time-critical applications
Counting Sort: Very useful when the keys have small range; stable;
memory space for counters and for 2n records.
Radix Sort: Appropriate for keys either rather short or with an
lexicographic collating sequence. 12
Bucket Sort: Assuming keys to have uniform distribution.
Summary of Sorting Algorithms
• Comparison Based Sorting Algorithms
13
Summary of Sorting Algorithms
• Non-Comparison Based Sorting Algorithms
O(N + K)
14
Summary of Sorting Algorithms
• In-place and Stable sorting Algorithms
15