Php determines whether an array is ordered. Php: how to determine whether an array is ordered. This article mainly introduces php's method of determining whether an array is ordered. it involves php's skills in array traversal, very practical php method to determine whether an array is ordered
This article mainly introduces php's method of determining whether an array is ordered. it involves php's skills related to array traversal and is very useful. For more information, see
This example describes how php judges whether an array is ordered. Share it with you 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
21
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 case
$ Array = array (
1,
2,
3,
4,
6,
5
);
$ Ret = JudegSortArray ($ array );
Echo $ ret;
|
I hope this article will help you with php programming.
This article mainly introduces php's method of determining whether an array is ordered. it involves php's skills related to array traversal, which is very useful...