Home > Backend Development > PHP Tutorial > PHP determines whether an array is ordered_PHP tutorial

PHP determines whether an array is ordered_PHP tutorial

WBOY
Release: 2016-07-13 17:54:22
Original
835 people have browsed it

Today is the last day of the National Day holiday. I have no intention of working on business code. When I saw a discussion in the group about how PHP determines the order of an array, I implemented an algorithm with a complexity of N and posted the code for everyone to learn

[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;
                                                                                                                                          $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;




http://www.bkjia.com/PHPjc/477957.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/477957.html

Today is the last day of the National Day holiday. I have no intention of working on business code. I saw a discussion in the group about how PHP determines the order of an array. , I implemented an algorithm with a complexity of N and posted the code for everyone...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template