IsSorted using SIMD in .NET07/11/2026
Sorting a set or an array is a relatively expensive operation. The costs of sorting typically increase non-linearly with the input size. Therefore, in certain cases, it can be beneficial to check if an array is already sorted before attempting to sort it.
Note: Different sorting algorithms handle sorted or 'nearly sorted' collections with varying degrees of efficiency.
For perspective, sorting an already sorted integer array with a million elements using Array.Sort in .NET takes 5,180.9 us on my machine. In contrast, validating if the same array is sorted takes only 119.0 us. For applications where most input arrays are expected to be sorted, validating sorted-ness first can be a worthwhile optimization.
Similarly, an operation like binary search might want to validate the precondition of the input array being sorted.