PHP determines whether an array is an ordered method _php tutorial

Source: Internet
Author: User

PHP determines whether an array is an ordered method


This article mainly introduces PHP to determine whether an array is an ordered method, involving the PHP operation array traversal related techniques, very practical value, the need for friends can refer to the next

This example describes how PHP determines whether an array is an ordered method. Share to everyone 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

21st

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 Cases

$array = Array (

1,

2,

3,

4,

6,

5

);

$ret = Judegsortarray ($array);

Echo $ret;

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/975116.html www.bkjia.com true http://www.bkjia.com/PHPjc/975116.html techarticle PHP Determines whether an array is an ordered method this article mainly introduces PHP to determine whether an array is an ordered method, involving the PHP operation array traversal related techniques, very practical price ...

  • 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.