How can I check if TArray is already sorted? I am using the default TArray.Sort to sort my array.
Check neighbour pairs with the same comparator as Sort uses
Result := True;
for i := Low(Arr) + 1 to High(Arr) do
if Compare(Arr[i], Arr[i - 1]) < 0 then
Exit(False);
It takes O(n) time (against O(nlogn) for sorting)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With