5

Does anyone know an Algorithm that sorts k-approximately an array?

We were asked to find and Algorithm for k-approximate sorting, and it should run in O(n log(n/k)). but I can't seem to find any.

K-approx. sorting means that an array and any 1 <= i <= n-k such that sum a[j] <= sum a[j] i<=j<= i+k-1 i+1<=j<= i+k

anatolyg
  • 26,506
  • 9
  • 60
  • 134
  • I'm wondering if the goal here is to separates n element into k groups so that all elements in group 0 are smaller than all elements of group 1 , group 1 elements smaller than group 2 elements, ... group k-2 elements smaller than group k-1 elements. The elements in each group would not be sorted. This can be done using quick select using median of medians (n/5 version), – rcgldr Nov 09 '15 at 14:33
  • No not really. K-approx. sorting means that an array and any 1<= i <= n-k such that sum a[j] <= sum a[j] i<=j<= i+k-1 i+1<=j<= i+k – Abdullah Raya Nov 10 '15 at 17:33
  • "The idea of the ASort algorithm is to partition the products into a sorted sequence of equal-sized bins such that the elements in each bin have smaller rank than any element in subsequent bins" - from [approximate sorting pdf](http://www.inf.ethz.ch/personal/smilos/approx.pdf) . I did a search for K-approximate sorting, but only got hits on approximate sorting, so maybe I'm missing something here. – rcgldr Nov 10 '15 at 19:53

1 Answers1

3

I know I'm very late to the question ... But under the assumption that k is some approximation value between 0 and 1 (when 0 is completely unsorted and 1 is perfectly sorted) surely the answer to this is quicksort (or mergesort).

Consider the following array:

[4, 6, 9, 1, 10, 8, 2, 7, 5, 3]

Let's say this array is 'unsorted' - now apply one iteration of quicksort to this array with the (length[array]/2)th element as a pivot: length[array]/2 = 5. So the 5th element is our pivot (i.e. 8):

[4, 6, 2, 1, 3, 9, 7, 10, 8]

Now this is array is not sorted - but it is more sorted than one iteration ago, i.e. its approximately sorted but for a low approximation, i.e. a low value of k. Repeat this step again on the two halves of the array and it becomes more sorted. As k increases towards 1 - i.e. perfectly sorted - the complexity becomes O(N log(N/1)) = O(N log(N)).

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
John Devitt
  • 690
  • 2
  • 8
  • 22