PHP determines whether an array is an ordered method, and PHP determines the array
This example describes how PHP determines whether an array is an ordered method. Share to everyone for your reference. The specific analysis is as follows:
The time complexity of this code is O (n)
<?php function Judegsortarray ($array) { if ($array [0] > $array [1]) { $flag = 1; } else { $flag = 0 ; } $temp = $flag; $len = count ($array); for ($i = 1; $i < $len; $i + +) {if ($flag = = 0) { if ($array [$i] < $array [$i + 1]) { continue;
} else { $flag = 1; break; } } if ($flag = = 1) { if ($array [$i] > $array [$i + 1]) { continue; } else { $flag = 0; Break ; }}} if ($flag! = $temp) { echo "unordered array"; } else { echo "ordered array";} } Test Case $array = Array ( 1, 2, 3, 4, 6, 5); $ret = Judegsortarray ($array); Echo $ret;
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/974674.html www.bkjia.com true http://www.bkjia.com/PHPjc/974674.html techarticle PHP determines whether an array is an ordered method, and the PHP judgment array describes how PHP determines whether an array is an orderly method. Share to everyone for your reference. The specific analysis is as follows ...