-2

Assume that we have an Mystery-Sort(A), which takes an array A of length n as input, sorts the numbers in A in non-decreasing order, and returns the sorted array.

We do not know whether the algorithm implemented by Mystery-Sort is stable. I need a procedure that takes an array of n integers and returns the sorted array in non-decreasing order, but i need the procedure to be stable.

How can i achieve this the pseudo-code of a stable sorting procedure Stable-Sort(A), which pre-processes and/or postprocesses the elements in A in O(n) time, makes only one call to Mystery-Sort, and returns the sorted array in non-decreasing order.

qwers
  • 41
  • 1
  • 6
  • So with non-descending order, you mean ascending order? Well - a stable sort which sorts integers can only be stable in respect to equal values, so for (1, 3, 3, 5, 2) it may sort them as (1, 2, 3, 3, 5), and if you could colorize the two 3s, one blue, one green, then you could check, whether both 3s are still in the same order. But since ordinary numbers don't have colors, you can't distinguish them. Or didn't I get the question? You can, of course, order by other criterias, for example prime numbers first, then non prime numbers. Or by number of factors. But without knowing the criterion? – user unknown Mar 19 '18 at 20:21
  • Yes, i mean ascending order. – qwers Mar 19 '18 at 20:27
  • @userunknown: If there are equal values, you *can't* sort into strictly ascending order -- hence the term "non-descending". – Prune Mar 19 '18 at 20:31
  • Depending on the language, if you don't have core values, but variables, and two independent variables are referring to 3, but you can distinguish them by their address/reference, you might check if two variables referring to the same value kept their order. – user unknown Mar 19 '18 at 20:33
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [On topic](http://stackoverflow.com/help/on-topic) and [how to ask](http://stackoverflow.com/help/how-to-ask) apply here. Most of all, do your research: what did you *not* get from the many discussions of stable sorts available with a browser search? – Prune Mar 19 '18 at 20:33
  • Also, please clarify your problem: do you need a new sort that's stable (as implied by the 2nd paragraph alone), or do you need a process that "fools" Mystery-Sort into producing stability, or simply corrects its return? – Prune Mar 19 '18 at 20:35
  • @Prune: i searched about stable sort but any of discussion does not answer my question or help me. Basically i need a function that uses Mystery-Sort once and output sorted A in a stable form. We need to perform preprocesses and/or postprocesses in that function with one Mystery-Sort. Your second argument is closer to what i want. – qwers Mar 19 '18 at 20:37
  • There is no difference between stable and unstable sorts when sorting integers... but the common way to do something like what you want is to sort items using a compound key that consists of the item's value and its original index. All keys are unique so it doesn't matter if the sort is stable or not. – Matt Timmermans Mar 20 '18 at 03:19

1 Answers1

1

I see this as coming in two phases: a pre-processing phase in which you find all duplicated elements with their identifiers; a post-processing phase where you simply overwrite the found elements into their original order.

You didn't specify how you can differentiate elements that sort as the same value; I'll call that the id. In this first pass, construct a table with one row per value. Iterate through the array; store the id of each element (or the entire element) in the matching table row in the table. If there's already an element there, extend that row and store the current element.

At this point, if you wish, you can eliminate any row of the table with fewer than 2 elements.

For the post-procesing, iterate through the sorted array. If the value you find is in the table, then don't trust the order returned from Mystery-Sort. Instead, simply overwrite the next elements with the ones from that row of the table. This restores their original order.

When you reach the end of the sorted list, you're done.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • stable sorting procedure Stable-Sort(A), which pre-processes and/or postprocesses the elements in A should work in O(n) time. – qwers Mar 19 '18 at 21:43
  • Right. Assuming that `n` is the length of `A`, this is two passes of **O(n)**. – Prune Mar 19 '18 at 21:44
  • Unless you have a way to find a given row in the table in `O(1)`, the first step would end up being `O(n^2)`, would it not? You'd be doing up to `n` table insertions, and if you are searching the `id`s linearly, there will be up to `n` `id`s also. My basic idea was the same as yours, but with some sort of map data structure instead of the table. – Tony Tuttle Mar 19 '18 at 22:06
  • @Prune I have an question that in the first pass, to determine whether there's already element should we do another iteration on table? I mean inside the iteration through array, can iteration on table change the O(n)? – qwers Mar 19 '18 at 22:07
  • You don't iterate on the table: you index directly to the row for that value. – Prune Mar 19 '18 at 22:15
  • Yes, a table has direct indexing: value as index, hash, translation directory, whatever ... – Prune Mar 19 '18 at 22:16