Today is the last day of the National Day holiday. I did not care about the Business Code. When I saw the discussion in the Group about how php judges how the arrays are ordered, I implemented an algorithm with the complexity of N and pasted the code for everyone to learn.
[Php]
<? 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;