Any fool can write code that a computer can understand. Good programmers write code that humans can understand. Martin Fowler
Bucket sorting is a sorting algorithm that works with partitioning an array into a number of packets. Each bucket sorted individually using the same or different algorithm. Bucket sort and radix sort look like same. But it has some difference.
The following table shows the difference between Bucket sort and radix sort.
|Bucket Sort||Radix Sort|
|Bucket sort adds the bucket and appends into one array.||Radix sort adds to the bucket without sorting or without bucket based on the second digit.|
|Stable sorting algorithm||Stable sorting algorithm|
|Separates array into smaller groups or buckets and sorts them individually using a sub-sorting algorithm or recursive||Radix means base (binary, octal, decimal, etc). Therefore, this sorting is for numbers (also used for strings|
|Combine step is trivial||Start from Most Significant Digit or Least Significant Digit|
|It uses buckets or bins of the fixed range.||Call counting sort on the array at the digit position|
Write a program to implement bucket sort algorithm.
Unsorted array before sorting: [1, 3, 4, 6, 4, 2, 9, 1, 2, 9] Sorted array after bucket Sort sorting: [1, 1, 2, 2, 3, 4, 4, 6, 9, 9]