Home     Blog

Bucket/Radix Sort

Most people use the bucket sort method when sorting a list of names in alphabetical order. The procedure is:

  • First, the names are grouped according to the first letter, thus the names are arranged in 26 classes, one for each letter of the alphabet. The first class consists of those names that begin with letter A, the second class consists of those names that begin with letter B, and so on.
  • Next, the names are grouped according to the second letter. After this step, the list of names will be sorted on the first two letters.
  • This process is continued for a number of times equal to the length of the name with maximum letters.

Since there are 26 letters in the alphabet, we make use of 26 buckets, one for each letter of the alphabet. After grouping these names according to their specific letter, we collect them according to the order of the buckets. This new list becomes the input for the next pass when we consider the next letter.

To sort decimal numbers where the base (or radix) is 10, we need 10 buckets, numbered from 0-9. Unlike sorting names, decimal numbers are sorted from right to left i.e. first on unit digits, then on ten digits and so on.

Example

Here is an illustration of how bucket sorting works. Consider the following unsorted array:

321 150 235 65 573 789 928 542

bucket sort clip image002 Bucket/Radix Sort

bucket sort clip image004 Bucket/Radix Sort

bucket sort clip image006 Bucket/Radix Sort

After pass three, when the numbers are collected, they are in the following order:

65 150 235 321 542 573 789 928

Thus, the numbers are sorted.

Algorithm

Bucketsort(a,n)

Here a is a linear array of integers with n elements, the variable digitcount is used to store the number of digits in the largest number in order to control the number of passes to be performed.

Begin
    find the largest number of the array
    set digitcount=no. of digits of the largest no.
    for pass=1 to digitcount by 1 do
        initialize buckets
        for i=1 to n-1 by 1 do
            set digit=obtain digit no. pass of a[i]
            put a[i] in bucket no. digit
            increment bucket count for bucket no. digit
        endfor
        collect all the numbers from buckets in order
    endfor
end

Analysis of Bucket Sort

Suppose that the number of digits in the largest number of the given array is s and the number of passes to be performed is n. Then, the number of comparisons, f(n), needed to sort the given array are:
f(n)=n*s*10, where 10 is the decimal number base.

Though s is independent of n, but if s=n, then in worst case, f(n)=O(n2).

But on the other hand, if s=log10 n, hen f(n)=O(n log10 n).

From the above discussion, we conclude that bucket sort performs well only when the number of digits in the elements are very small.

VN:F [1.9.17_1161]
Rating: 0.0/10 (0 votes cast)
Follow Will.Spencer on

Leave a Reply

Related Posts

  • Merge Sort

    Merging means combining elements of two arrays to form a new array. The simplest way of merging two arrays is to first copy all the elements of one array into a new array and then append all the elements of the second array to the new array. If you want the resultant array to be [...]...


  • How to Use the Unix Sort Command

    The Unix sort command is a command for the Unix family of operating systems. It is designed to sort whatever information you give it. The command can be used for a variety of purposes, but it is most frequently employed when there are a number of different files which need to be ordered in some [...]...


  • Insertion Sort

    This algorithm is very popular with bridge players when they sort their cards. In this procedure, we pick up a particular value and then insert it at the appropriate place in the sorted sub list. This algorithm requires n-1 passes. Pass1: a[1] is inserted either before or after a[0] so that a[0] and a[1] are [...]...


  • Shell Sort

    Invented by Donald Shell in 1959, Shell sort is the most efficient of the O(n2) class of sorting algorithms. It is also the most complex of the O(n2) algorithms. This algorithm is similar to bubble sort in the sense that it also moves elements by exchanges. Shell sort begins by comparing elements that are at [...]...


  • Selection Sort

    Selection sort requires (n-1) passes to sort an array. In the first pass, we find the smallest element from a[0], a[1], a[2], … a[n-1] and swap it with the first element, i.e. a[0]. In the second pass, we find the smallest element from a[1], a[2], a[3]….a[n-1] and swap it with a[1] and so on. Pass1. [...]...