0% found this document useful (0 votes)
54 views

Sorting in Linear Time: Counting Sort, Radix Sort

Counting sort and radix sort are linear time sorting algorithms. Counting sort works by counting the number of elements less than each input element and placing each element in the output array based on that count. Radix sort sorts elements based on the individual digits of multi-digit numbers, performing digit-based passes to fully sort the numbers with only d passes where d is the number of digits. Radix sort is stable and sorts on the least significant digit first before moving to more significant digits.

Uploaded by

Mia Sam
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Sorting in Linear Time: Counting Sort, Radix Sort

Counting sort and radix sort are linear time sorting algorithms. Counting sort works by counting the number of elements less than each input element and placing each element in the output array based on that count. Radix sort sorts elements based on the individual digits of multi-digit numbers, performing digit-based passes to fully sort the numbers with only d passes where d is the number of digits. Radix sort is stable and sorts on the least significant digit first before moving to more significant digits.

Uploaded by

Mia Sam
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

SORTING IN LINEAR TIME

Counting Sort, Radix Sort

Counting Sort

Counting sort assumes that each of the n input elements is an integer in the range 0 to k, for some integer k. When k=O(n), the sort runs in theta(n) time. Counting sort determines, for each input element x, the number of elements less than x. It uses this information to place element x directly into its position in the output array. For example, if 17 elements are less than x, then x belongs in output position 18.

Counting Sort Con.

We must modify this scheme slightly to handle the situation in which several elements have the same value, since we do not want to put them all in the same position. In the code for counting sort, we assume that the input is an array A[1n], and thus A.length =n. We require two other arrays: the array B[1..n] holds the sorted output, and the array C[0..k] provides temporary working storage.

Counting Sort Con..

Counting Sort Algorithm

Properties of Counting Sort

It is not a comparison sort. In fact, no comparisons between input elements occur anywhere in the code. Instead, counting sort uses the actual values of the elements to index into an array. It is stable which means numbers with the same value appear in the output array in the same order as they do in the input array.

Radix Sort

Radix sort is the algorithm used by the cardsorting machines you now find only in computer museums. The cards have 80 columns, and in each column a machine can punch a hole in one of 12 places. The sorter can be mechanically programmed to examine a given column of each card in a deck and distribute the card into one of 12 bins depending on which place has been punched. An operator can then gather the cards bin by bin, so that cards with the first place punched are on top of cards with the second place punched, and so on.

Radix Sort Con.

For decimal digits, each column uses only 10 places. (The other two places are reserved for encoding nonnumeric characters.) A d-digit number would then occupy a field of d columns. Since the card sorter can look at only one column at a time, the problem of sorting n cards on a d-digit number requires a sorting algorithm. Intuitively, you might sort numbers on their most significant digit, sort each of the resulting bins recursively, and then combine the decks in order. Unfortunately, since the cards in 9 of the 10 bins must be put aside to sort each of the bins, this procedure generates many intermediate piles of cards that you would have to keep track of

Radix Sort Con.

Radix sort sorts on the least significant digit first. The algorithm then combines the cards into a single deck, with the cards in the 0 bin preceding the cards in the 1 bin preceding the cards in the 2 bin, and so on. Then it sorts the entire deck again on the second-least significant digit and recombines the deck in a like manner. The process continues until the cards have been sorted on all d digits. Remarkably, at that point the cards are fully sorted on the d-digit number. Thus, only d passes through the deck are required to sort.

Radix Sort Visualization

Radix Sort Con..

In order for radix sort to work correctly, the digit sorts must be stable. The sort performed by a card sorter is stable, but the operator has to be wary about not changing the order of the cards as they come out of a bin, even though all the cards in a bin have the same digit in the chosen column.

Usage of Radix Sort

In a typical computer, which is a sequential random-access machine, we sometimes use radix sort to sort records of information that are keyed by multiple fields. For example, we might wish to sort dates by three keys: year, month, and day. We could run a sorting algorithm with a comparison function that, given two dates, compares years, and if there is a tie, compares months, and if another tie occurs, compares days. Alternatively, we could sort the information three times with a stable sort: first on day, next on month, and finally on year.

Radix Sort Algorithm

The code for radix sort is straightforward. The following procedure assumes that each element in the n-element array A has d digits, where digit 1 is the lowest-order digit and digit d is the highest-order digit.

You might also like