Lecture 15 - Bucket Sort
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.
• 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
.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
.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.
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
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
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