PHP determines whether an array is an ordered method
This article mainly introduces PHP to determine whether an array is an ordered method, involving the PHP operation array traversal related techniques, very practical value, the need for friends can refer to the next
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)
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 |
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 Cases
$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/975116.html www.bkjia.com true http://www.bkjia.com/PHPjc/975116.html techarticle PHP Determines whether an array is an ordered method this article mainly introduces PHP to determine whether an array is an ordered method, involving the PHP operation array traversal related techniques, very practical price ...