Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the best method to maintain or measure how well sorted a collection is so we can choose best sort algorithm?

Inspired by this question

The choice of which algorithm to use to sort a collection can be made better if we know ahead of time how well sorted a collection is. Is there a way we can measure (or maintain a measurement) of how well sorted the collection is? Can we do this in such a way that the cost of maintaining or measuring how well sorted something doesn't outweigh the benefit of choosing the best sort algorithm?

like image 767
Doug T. Avatar asked Dec 17 '22 10:12

Doug T.


1 Answers

Augmenting @Doug:

A deletion can never make the list less sorted, so you don't have to track those.

When an insertion happens, compare with the elements around to determine if this insertion was in-order or not. If yes, don't increase the counter. If no, increase the "not sorted" counter.

Perhaps this is too much of a penalty (i.e. two compares per insert). You could do only one compare for a more fuzzy result? Or I do like the idea of just counting inserts.

like image 176
Jason Cohen Avatar answered May 20 '23 15:05

Jason Cohen