Ok, first check if sorting is sorted by definition, which will always save you a lot of time :) For the most part, don’t worry about expanding the collection to check if it is sorted during insert / delete operations, if the collection needs to be sorted, use a collection that is sorted a-priory.
If you are trying to extend the collection class to track sorting, just keep a separate sorted list of pointers to items in the collection ...
Finally, for 99.99% of the time, why bother? Just use quicksort. If your dataset is small enough that the constant part of Big O sorting in quicksort will redefine the time series from bubble sorting, the sorting will be so fast that you don’t even have to waste time asking a question.
Are you really telling me that your question is 0.01% of the sort you need to solve?
source share