Php method to determine whether an array is ordered _ PHP Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.