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, post code for everyone to learn [php] & lt ;? PhpfunctionJudegSortArray ($ array) {if ($ array [0] & gt;... Synt
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]
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;