This article mainly introduces the method in PHP to determine whether an array is ordered, and involves related techniques for operating array traversal in PHP , very practical value, friends in need can refer to it
The example in this article describes how PHP determines whether an array is ordered. Share it with everyone for your reference. The specific analysis is as follows:
The time complexity of this code is O(n)
?
3 4 513 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; |