Fastest way to check if an array is sorted

后端 未结 9 2057
走了就别回头了
走了就别回头了 2020-12-05 19:36

Considering there is an array returned from a function which is of very large size.

What will be the fastest approach to test if the array is sorted?

9条回答
  •  再見小時候
    2020-12-05 20:19

    This might not be the fastest but it's the complete solution. Every value with index lower than i is checked against the current value at i. This is written in php but can easily be translated into c# or javascript

    for ($i = 1; $i < $tot; $i++) {
            for ($j = 0; $j <= $i; $j++) {
                //Check all previous values with indexes lower than $i
                if ($chekASCSort[$i - $j] > $chekASCSort[$i]) {
                    return false;
                }
            }
        }
    

提交回复
热议问题