22
$\begingroup$

Assume that we want to sort a list $S$ of $n$ real numbers. Assume that we are given a black box that can sort $\sqrt n$ real numbers instantly. How much advantage can we gain using this black box?

For example, can we sort the numbers with only $O(\sqrt n)$ calls to the black box? The best algorithm that I have found uses $n$ calls to the black box. But I haven't been able to improve it further. Here is my algorithm which is similar to merge-sort:

First partition the list $S$ into $\sqrt n$ lists $s_1, s_2, ..., s_{\sqrt n}$ with approximately $\sqrt n$ size. Then use $\sqrt n$ calls to the black box to sort these lists. Finally, merge the sorted lists using the black box as follows:

Put the smallest elements of the lists in a new list $L$, then call the black box to sort it. The number in $L[1]$ (first and smallest element of $L$) will be the smallest number in $S$. We can put it in the first place of the output list.
Assuming the element has been chosen from $s_j$, we replace $L[1]$ with the second smallest element of sort list $s_j$, and again run the black box on it to compute the second smallest member of $S$.
We continue till all the elements are sorted. The total number of black box calls for this part will be $n - \sqrt n$. Therefore overall the total number of calls will be $n$.

On the other hand, it looks like we should be able to obtain a lower-bound using the lower-bound on the number comparisons needed for sorting as follows: We can implement the black box using $\sqrt n \lg \sqrt n = \frac{1}{2} \sqrt n \lg n$ comparisons. If we can solve the problem with $o(\sqrt n)$ calls to the black box and merging in linear-time we can sort $n$ real numbers with $o(n \lg n)$ comparisons which is impossible.

I guess we could prove that $\Omega(n)$ is a lower-bound for the number of calls to the black box, since lots of comparisons using in the black box would be shared and therefore are recounted in our argument.

UPDATE: As the the other posts suggest, an $\sqrt n \lg n$ is also achievable.

$\endgroup$
14
  • 2
    $\begingroup$ There seems to be a typo in your comment. Did you mean to say: "no algorithm using less than $\sqrt{N}$ calls to the machine can sort $N$ real numbers with less than $N\lg N$ comparisons"? ps: you should also be careful about the fact that the $N\lg N$ lower-bound only holds for comparison based sorting algorithms. $\endgroup$
    – Kaveh
    Mar 17, 2013 at 15:54
  • 8
    $\begingroup$ I think we can even get $O(\sqrt{n}\log n)$ using AKS's sorting network. Their network can be thought of as an instantiation of your model where the black box can sort blocks of size 2. Their algorithms uses $O(\log n)$ rounds, each round calling the 2-sorter $O(n)$ times. One "round" of $O(n)$ 2-sorters can be easily simulated with $O(\sqrt{n})$ $\sqrt{n}$-sorters. $\endgroup$ Mar 17, 2013 at 17:49
  • 6
    $\begingroup$ @VinayakPathak: Break up the input data into $2\sqrt{N}$ chunks of size $\sqrt{N}/2$, and then sort the chunks using the AKS network, after replacing each comparator with a $\sqrt{N}$-sorter. $\endgroup$
    – Jeffε
    Mar 17, 2013 at 18:51
  • 1
    $\begingroup$ @JɛffE: Yes, great, that definitely looks simpler than my construction. $\endgroup$ Mar 17, 2013 at 20:06
  • 1
    $\begingroup$ @JɛffE, your comments can be an answer. :) $\endgroup$
    – Kaveh
    Mar 17, 2013 at 20:36

3 Answers 3

16
$\begingroup$

It's possible to sort with $O(\sqrt n\log n)$ calls to the black box and no comparisons.

First, consider the following balanced partitioning problem: given $m$ elements $A[1..m]$ (where $\sqrt n \le m \le n$), partition them into two groups, the smallest of size at least about $m/4$, so that all elements in the first group are smaller than all elements in the second group. This can be done with $O(m/\sqrt n)$ calls to the black box. (I'll describe that later.) Then use quicksort with this partitioning algorithm:

def qsort(A[1..m]):
   if m < sqrt(n): sort A with one call to the black box
   else:
     Partition A[1..m] into two groups as described above.
     Recursively qsort the first group.
     Recursively qsort the second group.

Assuming each partition step takes $O(m/\sqrt n)$ calls to the black box, the above algorithm, given input $A[1..n]$, will make $O(\sqrt n\log n)$ calls to the black box, because the recursion tree has depth $O(\log n)$ and each level of the tree has a total of $O(n/\sqrt n) = O(\sqrt n)$ calls to the black box.

Do the partitioning step as follows:

def partition(A[1..m]):  (where sqrt(n) <= m <= n)
   Divide A into m/sqrt(n) groups of size sqrt(n) each.
   Sort each group with one call to the black box per group.
   Sort the medians of the groups with one call to the black box.
   (Note the number of groups is less than sqrt(n), because m <= n.)
   Let X be the median of the medians.
   Partition all m elements around X, using the black box as follows:
      For each group G, let Y be its median:
        Call the black box once on (G - {Y}) union {X}.
        (This gives enough information to order all elts w.r.t. X.)
$\endgroup$
2
  • $\begingroup$ In the last step of the algorithm partition(): "Partition all m elements around X", won't this use m additional comparisions? $\endgroup$ Mar 17, 2013 at 17:59
  • 2
    $\begingroup$ See the line just following the algorithm, it explains how to do that step. I'll edit it to make that clearer. $\endgroup$
    – Neal Young
    Mar 17, 2013 at 18:00
24
$\begingroup$

I think your question was addressed in Beigel and Gill's paper "Sorting n objects using k-sorter" from 1990 and the abstract of the paper says it all:

A k-sorter is a device that sorts k objects in unit time. The complexity of an algorithm that uses a k-sorter is defined as the number of applications of the k-sorter. In this measure, the complexity of sorting n objects is between $\frac{n \log n}{k \log k}$ and $\frac{4n \log n}{k \log k}$, up to first-order terms in n and k.

$\endgroup$
4
  • $\begingroup$ Thank you. I couldn't find the paper. Can you provide the link to the paper or its proof? $\endgroup$
    – AmeerJ
    Mar 17, 2013 at 17:31
  • 6
    $\begingroup$ link to paper $\endgroup$
    – Neal Young
    Mar 17, 2013 at 17:50
  • $\begingroup$ Also on citeseerx. $\endgroup$
    – Kaveh
    Mar 17, 2013 at 20:43
  • 8
    $\begingroup$ Note that when $k=\sqrt{n}$, Beigel and Gill's bound is $\Theta(\sqrt{n})$. $\endgroup$
    – Jeffε
    Mar 18, 2013 at 16:49
12
$\begingroup$

It is possible to sort obliviously with $O(\sqrt{n}\log n)$ calls to the black box, each applied to a contiguous subarray of the original input. The algorithm never touches the input data except through black-box calls. In particular, the same sequence of black-box calls is only a function of $n$, not the actual input data (hence "oblivious").

Here is a sketch of a simpler algorithm, which uses a black box that sorts subarrays of size $k$ instead of just $\sqrt{n}$. The total number of calls to the black box is roughly $2(n/k)^2$. For notational simplicity, assume that $k$ is even and $2n/k$ is an odd integer.

BlockBubbleSort(X[0..n-1], k):
   m = floor(n/k)
   for i = 1 to m
      for j = 0 to m-1
          BlackBoxSort(X[j*k .. (j+1)*k-1])
      for j = 0 to m-1
          BlackBoxSort(X[j*k + k/2 .. (j+1)*k + k/2 - 1])

Here is a diagram of the algorithm for $n=18$ and $k=4$. Data travels from left to right; each box is a $k$-sorter.

enter image description here

As the figure hopefully suggests, this algorithm breaks the input array into chunks of size $k/2$, and then applies bubblesort to the chunks, using the $k$-sorter in place of a compare-exchange operation. Correctness follows by observing that the network correctly sorts any array of 0s and 1s.

As @VinayakPathak's comment suggests, the $O((n/k)^2)$ bound can be reduced by replacing bubblesort with a different sorting network. For example, Batcher's even-odd mergesort reduces the number of black-box calls to $O((n/k)\log^2(n/k)) = O(\sqrt{n}\log^2 n)$, and the AKS sorting network reduces it to $O((n/k)\log (n/k)) = O(\sqrt{n}\log n)$. This last algorithm matches Neal's non-oblivious algorithm up to (large!!) constant factors.

$\endgroup$
4
  • $\begingroup$ thanks. Can a $\Omega(\sqrt n\ lg(n))$ lower bound be proved? $\endgroup$
    – AmeerJ
    Mar 20, 2013 at 17:43
  • 2
    $\begingroup$ No, Biegel and Gill's paper implies an upper bound of $O(\sqrt{n})$. $\endgroup$
    – Jeffε
    Mar 20, 2013 at 18:47
  • $\begingroup$ +1 for that nice picture! I may be mistaken but it seems to me that it is not entirely correct (correct me if I'm wrong). Assuming the output is in ascending order, if the smallest element is in the last position there is no "path" for it to "travel" to the first position. Changing "for i = 1 to m" to "for i = 1 to m+1" seems to correct this, although it might introduce some unnecessary overhead. $\endgroup$
    – George
    Mar 20, 2013 at 21:22
  • $\begingroup$ @George Oops, you're right; I need one more layer! $\endgroup$
    – Jeffε
    Mar 21, 2013 at 5:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.