2

Let R1,...Rn be n axis-aligned rectangles in the plane for which the corners are points in the n×n-grid. Thus, for each rectangle Ri the four corners are points where both coordinates are integers in {1,...n}.

Now I want to sort these rectangles R1,...Rn by there increasing area in O(n) time.

I have an algorithm to sort them in O(n*log n). But how can it be done in O(n) ?

Using O(n*log n) we can do this :

Calculate all the areas, and then sort using any standard sorting algorithm like Quick sort

I guess some per-processing will be required, so that we can sort in O(n) , because we are given some pre-conditions which can help. I just want algorithm, no code required.

ms8
  • 327
  • 1
  • 10
  • 1
    As the resulting areas are integer numbers, you can implement a [radix sort](http://stackoverflow.com/q/14717560/4856258) algorithm. – Tagir Valeev Jun 15 '15 at 04:56
  • @TagirValeev How radix sort is going to help here? – ms8 Jun 15 '15 at 04:57
  • 1
    @python_slayer A radix sort is a [*non-comparison sort*](https://en.wikipedia.org/?title=Radix_sort) and runs in "O(n)" when k << n (see link for details). The O(n lg n) lower bounds are for a *comparison* sort. – user2864740 Jun 15 '15 at 04:58
  • @TagirValeev Whats better ? Counting sort or radix sort in this case – ms8 Jun 15 '15 at 05:28
  • @python_slayer, I guess that counting sort is inappropriate in your case. – Tagir Valeev Jun 15 '15 at 05:41
  • @TagirValeev Reason ? Why inappropriate ? In answer below one comment is "You would use radix sort if you had representations of the integers in e.g., base 10 or base 2 notation. But you don't have representations of the integers, you have the integers themselves, so use counting sort. (Actually the answer depends on the size of n ... if it's bigger than maximum long integer you'd essentially have to use a radix sort, but that's highly unlikely). " – ms8 Jun 15 '15 at 05:43

1 Answers1

1

Since the keys (areas) of the rectangles are integers, the task can be completed in O(n) time using a counting sort. You know the minimum key is 0 and maximum key for the problem is n^2, so in the algorithm k=n^2+1. The algorithm completes in three passes: computing histogram, computing starting and ending indexes for each key, then copying the data to the output array, preserving order of inputs with equal keys so that the sort is stable. Each pass is O(n) so altogether the algorithm is O(n).

Example: Suppose n is 3. k is one more than the largest key that appears in the data, so that all keys fit in the range [0..k-1] inclusive, i.e., k is 10. You make a histogram h by setting up an array of 0s with index from 0 to k-1, and you fill the histogram by walking through your set of rectangles and just count them up. Say there are 2 with area 1, 5 with area 2, and 2 with area 4. h = [0, 2, 5, 0, 2, 0, 0, 0, 0, 0]. Then the starting indexes are immediately computed from the histogram as 0, 0, 2, 7, 7, 9, 9, 9, 9, 9. Any rectangle with area 0 goes into output array starting at 0. Any rectangle with area 1 goes into output array starting at 0 (and increment that number when you put a rectangle of area 1 into output). Any rectangle with area 2 goes into output array starting at 2. Any rectangle with area 3 goes into output array starting at 7. Any rectangle with area 4 goes into output array starting at 7.

Edward Doolittle
  • 3,770
  • 2
  • 12
  • 24
  • Can you explain by some example ? Whats K here ? – ms8 Jun 15 '15 at 05:11
  • What will be better radix sort or counting sort ? – ms8 Jun 15 '15 at 05:23
  • I gave an example. You would use radix sort if you had representations of the integers in e.g., base 10 or base 2 notation. But you don't have representations of the integers, you have the integers themselves, so use counting sort. (Actually the answer depends on the size of n ... if it's bigger than maximum long integer you'd essentially have to use a radix sort, but that's highly unlikely). – Edward Doolittle Jun 15 '15 at 05:33
  • I just noticed my example has wrong number of rectangles (should be n, as per OP). I'll correct shortly. – Edward Doolittle Jun 15 '15 at 05:50
  • I don't get why it's in O(n). Doesn't the wikipedia page say it's in O(k+n)? So if k = n^2 + 1, doesn't that mean the algorithm is in O(n^2 + n) = O(n^2)? – user3472 May 28 '21 at 15:37