Morwenn / cpp-sort

Sorting algorithms & related tools for C++14
MIT License
624 stars 58 forks source link

Better complexity for measures of presortedness #85

Open Morwenn opened 8 years ago

Morwenn commented 8 years ago

According to Computing and ranking measures of presortedness by J. Chen, the complexity of some of our measures of presortedness simply outrightly suck. With better algorithms, we could change them as follows, (quotes are from the paper):

Also, some of the comments make me doubt the implementation of the of the measures of presortedness. Did I really understand every definition, or did I take some mental shortcuts? It's left to investigate... I'll gladly accept any help if anyone knows how to implement these algorithms with the complexities above.

Morwenn commented 3 years ago

As explained in this lengthy comment, the measure of presortedness Radius(X) described in by T. Altman and Y. Igarashi Roughly Sorting: Sequential and Parallel Approach is pretty much the same as Par(X) described by V. Estivill-Castro and D. Wood in A New Measure of Presortedness, which means that we won't implement a probe::radius in cpp-sort.

However the good news is that the Roughly Sorting papers gives a linear method to compute Radius(X), which means that we can surely change our Par(X) implementation. The described method has the following properties:

Having an O(n² log n) algorithm is kind of unsightly, but as can be seen in the benchmarks, the current implementation of Par(X) is a sneaky beast with a high enough short-circuiting power that it manages to run faster than O(n) algorithms when it doesn't encounter an adverse pattern. It would be nice if we could take advantage of the short-circuits while running in O(n) time, but I fear that it isn't possible.

Morwenn commented 3 years ago

Commit 77c4c756d1cff0ce3f2566b4a942fa10fbad9270 changes probe::osc to run in O(n log n) time and O(n) space. Thanks a lot to @Control55 from @The-Studio-Discord for coming up with the new algorithm.