Under what conditions do these non-comparison sorts run in linear time?
Asked Answered
V

1

7

I am exploring the following algorithms:

  1. Counting Sort
  2. Radix Sort
  3. Bucket Sort

I know all three are capable of running in linear time at best case, but I am having trouble with understanding when those cases occur, except in the case of counting sort.

Here is my understanding of counting sort, and how I would like an answer for the other two algorithms if possible:

Counting sort runs in linear time when there is not large gaps between the information you wish to be sorted. For example 1, 10^5, and 545, etc would create a large array and not be efficient using the memory and traversal so this would be the "worse case" to use a counting sort, and therefore the best case would be when the gaps are small.

If anyone could help me to understand the conditions for radix and bucket sort best cases when linear time occurs I would be grateful.

Vixen answered 2/4, 2013 at 3:42 Comment(6)
Radix, IIRC, is always linear, but the constant factor is the number of bits in the largest integer. So a 32-bit radix sort runs in 32 linear passes over the data. For N < 2^32, an N lg N sort like mergesort will be faster.Blatant
When you say "linear time," what parameter are you trying to be linear in? The number of elements? The number of bits in the largest value? The total number of bits?Pressor
@Pressor I am talking about the run time of the algorithm. As judge states, yes, they're all linear time, but I'm wanting to understand when the optimal case would be, run time wiseVixen
@JudgeMental, radix sort can work on multiple bits per pass if properly coded.Bowrah
@ZAX- Sorry, let me try to clarify. I understand that you're talking about the runtime, but I was curious what you meant by "linear time." An algorithm could be linear in the total number of elements (O(n)), or linear in the number of bits in the largest element (say, O(n + log U)). Are you referring specifically to O(n) time?Pressor
@Pressor Sorry if I'm not clear. I'm not sure exactly, but I believe just O(n) in general for the algorithm to run. I am still learning big O but have not brushed the surface of the number of bits issue you are discussingVixen
P
8

Let's start off by analyzing each algorithm independently and seeing if we can determine in what cases they will run in linear time.

First, let's look at counting sort. The algorithm works as follows:

  • Find the largest integer to be sorted.
  • Create an array with one slot per integer to sort.
  • Iterate across the main array, updating the second array with the frequencies of each element.
  • Construct the sorted array by filling in the output array with the appropriate number of copies of each element.

The first step can be done in time O(n) by iterating over the main array. Let's suppose that the largest value in the array is U. In that case, step two takes time O(U), since we have to zero out the array elements. The third step takes time O(n). The final step then takes time O(n + U), since we visit each element of the frequencies array exactly once (O(U)) and make a total of n writes to the output array O(n). This means that the total runtime is O(n + U). Note that this depends both on the total number of elements that need to be sorted (larger arrays take longer to sort) and the sizes of the elements in the array (bigger numbers will require proportionally more runtime).

If we want this runtime to be O(n), we need to require that U = O(n). That way, we would have that O(n + U) = O(n + n) = O(n). This means that you need to have the size of the largest element in the array be growing at about the same rate that the length of the array is increasing. For example, if you're guaranteed that all elements in the array are in the range 1 .. n, then counting sort will take time O(n) to complete. It doesn't matter how those elements are distributed within that range.


Radix sort essentially works by repeatedly doing a counting sort over and over again, once for each digit of the numbers in the array to sort. The key idea behind radix sort is to keep the value of U from the previous algorithm as low as possible. By picking some fixed base, the value of U is capped at some constant. For example, if you use binary radix sort and go one bit at a time, each element of the array to sort is, essentially, treated as either a 0 or as a 1. This means that each round of radix sort takes time O(n) to complete. The number of rounds required to sort the entire array is then given by the number of digits in the largest number in the array, which is Θ(log U). This means that the total runtime of the algorithm ends up being O(n log U). Note again that the runtime depends on the number of elements and on the size of the biggest element in the array.

The advantage this time is that log U grows much, much more slowly than U. For example, 2300 is about the total number of atoms in the universe, but lg (2300) = 300, which is quite small.

Some people will claim that log U is a constant on any fixed computer. On a 32-bit computer, all integers are 32-bits (unless you're using arbitrary-precision integer libraries, which we'll ignore for now), and on a 64-bit system all integers are 64 bits. In the first case, log U = 32, and in the second log U = 64. You could claim that for a fixed system, radix sort always takes linear time, since the runtime will be O(n). The constant varies from system to system, though. If you're more theoretically minded and want to be more precise, you could say that radix sort runs in linear time as long as log U = O(1), since that way O(n log U) = O(n). This means that if you have any constant upper bound on the number of bits in the number, you're guaranteed that radix sort will run in linear time.


The bucket sort algorithm is similar to radix sort, except that it distributes elements using the most significant digit rather than the least significant digit. This means that the analysis is pretty much the same as before - as long as log U = O(1), the algorithm runs in linear time.


Hope this helps!

Pressor answered 2/4, 2013 at 4:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.