Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between bucket sort and radix sort?

Bucket sort and radix sort are close cousins; bucket sort goes from MSD to LSD, while radix sort can go in both "directions" (LSD or MSD). How do both algorithms work, and in particular how do they differ?

like image 774
Lazarus Avatar asked Dec 16 '10 14:12

Lazarus


People also ask

Is bucket sort faster than radix sort?

It was found that bucket sort was faster than RADIX sort, but that bucket sort uses more memory in most cases. The sorting algorithms performed faster with smaller integers. The RADIX sort was not quicker with already sorted inputs, but the bucket sort was.

Is bucket sort used in radix sort?

Radix-sort is a specialization of lexicographic-sort that uses bucket-sort as the stable sorting algorithm in each dimension.

Why bucket and radix sort can perform better than comparison sorts?

The time complexity will be Θ(d*(n + b)) where b is the base and n the is the number of elements. In comparison with radix sort and counting sort, bucket sort works in linear time and is the better algorithm when the data is perfectly distributed across a range.

What is the difference between radix sort and counting sort?

Radix sort is a specific type of bucket sort. It starts with the top n-bit or n-digits and may sort those buckets using a radix sort etc., until every entry is sorted. Counting sort is like using radix sort except you are using the whole value.


2 Answers

The initial pass of both RadixSort and BucketSort is exactly the same. The elements are put in buckets (or bins) of incremental ranges (e.g. 0-10, 11-20, ... 90-100), depending on the number of digits in the largest number.

In the next pass, however, BucketSort orders up these 'buckets' and appends them into one array. However, RadixSort appends the buckets without further sorting and 're-buckets' it based on the second digit (ten's place) of the numbers. Hence, BucketSort is more efficient for 'Dense' arrays, while RadixSort can handle sparse (well, not exactly sparse, but spaced-out) arrays well.

like image 53
Akash Avatar answered Oct 14 '22 16:10

Akash


Bucket Sort and Radix Sort are like sister sorting algorithms because they are not comparison sorts and the general idea is similar. Also, they both are a bit abstract in implementation.

Radix Sort:

  • Radix means base(binary, octal, decimal,etc). Therefore, this sorting is for numbers (also used for strings). This works when each element E is like ek...e2e1e0, where ei is in some range. (usually 0 to a base like 0-9 in decimal or 0-255 in ASCII characters)

  • It then uses k passes of a stable sub-sorting algorithm (It has to be stable or else Radix sort won't work) to sort the numbers. This sub-sorting algorithm is usually Counting sort or Bucket sort as well but it cannot be Radix sort itself.

  • You can start from Most Significant Digit or Least Significant Digit because it shuffles every number in each pass (from k to 0 or 0 to k)

  • It is a stable sorting algorithm.

Bucket Sort:

  • It separates array into smaller groups or buckets and sorts them individually using a sub-sorting algorithm or recursive call to itself and then combines the result. For example, sorting players by adding into buckets of their team then sorting them by their jersey numbers or something like sorting numbers from ranging from 1-30 into 3 buckets of 1-10, 11-20, 21-30.

  • The combine step is trivial (unlike merge sort). just copy elements of each bucket back into original array or join the head of each bucket with tail of previous bucket (in case of linked lists)

  • Radix/base could be a type/instance of the group/bucket while sorting numbers. Therefore you could think of MSD radix as a modified instance of bucket sort

  • Bucket sort is not in-place but stable sorting algorithm. However, some variations of bucket sort might not be stable (if you use a sub-sorting algorithm which is not stable)

like image 26
dev_ankit Avatar answered Oct 14 '22 17:10

dev_ankit