Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a black box method to detect if a sorting algorithm is stable?

In JavaScript (somewhat applicable elsewhere), where you don't know what target implementation your code is running on, is there a way to feature detect if the underlying sorting algorithm (of Array.sort) is stable or not, knowing only that it follows the specification?

I could find 2 tests in webkit (1) (2), but how reliable are those tests? (Could this check be done with a PCP?) I'm looking for a solution that would be mathematically sound.

This is a tricky problem, since a more advanced sorting algorithm can change subalgorithms depending on the length of the source array (like Timsort). I've been confused since every test I've run has shown Google Chrome's sort to be stable, but all documentation I've seen has said that it's unstable (the source will tell you why).

(Typically, I use this strategy to make my sorts stable; it has a small but sometimes noticable performance impact)

Source code for sorting in various implementations:
  • Google Chrome/ium - V8
  • Safari - JSC
  • Firefox - *Monkey (2) (3)
like image 749
forivall Avatar asked May 10 '13 01:05

forivall


People also ask

How do you test if a sorting algorithm is stable?

A sorting algorithm is stable if elements with the same key appear in the output array in the same order as they do in the input array. That is, it breaks ties between two elements by the rule that whichever element appears first in the input array appears first in the output array.

What is the black box method?

Black box testing involves testing a system with no prior knowledge of its internal workings. A tester provides an input, and observes the output generated by the system under test.

Which sorting method is not stable?

Stable and Unstable Sorting Algorithms Several common sorting algorithms are stable by nature, such as Merge Sort, Timsort, Counting Sort, Insertion Sort, and Bubble Sort. Others such as Quicksort, Heapsort and Selection Sort are unstable.


2 Answers

Black-box testing can not be used to determine that a program satisfies any criterion unless you can test all possible inputs relevant to the criterion. A black box is free to simply have a lookup table that maps inputs to outputs (see Pentium FDIV bug for a real-world lookup table bug) so you cannot be sure that your tests preclude the possibility of some other input triggering a violation.

like image 74
Sara Avatar answered Sep 28 '22 07:09

Sara


Mathematically sound, eh? That would require every path in the algorithm to be proved stable - and every combination of them. For any possible data.

Sure algorithms like that exist - but they were most probably made to fit that requirement. So if it is, it probably says it is somewhere.

As for a test to prove something like that, that probably falls under similar issues as a Halting problem.

http://en.wikipedia.org/wiki/Halting_problem

like image 40
MarZab Avatar answered Sep 28 '22 06:09

MarZab