Bubble and fast sorting algorithm in PHP ___ algorithm

Source: Internet
Author: User

1. System built-in sort function

<?php

//sorting algorithm

$arr = [3,1,7,9,5,4];
Sort ($arr);
Print_r ($arr);
Array
(
    [0] => 1
    [1] => 3
    [2] => 4
    [3] => 5
    [4] => 7
    [5] => 9
)

The above is a sort of simple data type, so sort the class object.

Class User
{public
    $age;
    Public function __construct ($age)
    {
        $this->age = $age;
    }
}

$arr = [New User (3), new User (1), new User (7), new User (9), new user
    (5), new user
    (4), c26/>];
Sort ($arr);
Print_r ($arr);

We found that the sort function can also sort the simple user class object

Array
(
    [0] => User Object
        (
            [age] => 1
        )

    [1] => the User object
        (
            [age] => 3
  )

    [2] => User Object
        (
            [age] => 4
        )

    [3] => User Object
        (
            [age] => 5
        )

    [4] => User Object
        (
            [age] => 7
        )

    [5] => User Object
        (
            [age] => 9
        )
)

If the user class is a little more complex:

Class User
{public
    $age;
    public $name;

    Public function __construct ($name, $age)
    {
        $this->age = $age;
        $this->name = $name;
    }
}

We can use another function Usort to specify the sort by age:

Usort ($arr, function ($a, $b) {
    if ($a->age = = $b->age) {return
        0;
    }
    Return $a->age > $b->age? 1:-1;
});

2, bubble sort

N numbers to sort, outer loop n-1, inner loop n-1-i, compare J and j+1, and swap

$arr = [3,1,7,9,5,4];
$n = count ($arr);
For ($i =0 $i < $n-1; $i + +) {for
    ($j =0; $j < $n-$i; $j + +) {
        if ($arr [$j] > $arr [$j +1]) {
            $temp = $arr [$j ];
            $arr [$j] = $arr [$j +1];
            $arr [$j +1] = $temp;}}}

Bubble sort is each number, checked individually. The algorithm is simple and stable and requires no extra space. But the efficiency is very low, the cycle is too much.

3. Quick Sort

Take the first one out, <= it, put the left, otherwise put the right. And then recursively, finally merging

function Quick_sort (array $arr) {
    if (count ($arr) <= 1) {return
        $arr;
    }

    $x = Array ();
    $y = Array ();
    $n = count ($arr);

    The first member
    $k = $arr [0];
    For ($i =1 $i < $n $i + +) {
        if ($arr [$i] <= $k) {
            $x [] = $arr [$i];
        } else{
            $y [] = $arr [$i];
        }
    }

    Recursion then sorts
    $x = Quick_sort ($x);
    $y = Quick_sort ($y);

     Return Array_merge ($x, Array ($k), $y); Here Merge to note the order
}
$arr = [3,1,7,9,5,4];
$NEWARR = Quick_sort ($arr);
Print_r ($NEWARR);
Array
(
    [0] => 1
    [1] => 3
    [2] => 4
    [3] => 5
    [4] => 7
    [5] => 9
)

Fast sorting is the most efficient sort algorithm, but it requires a lot of memory space.

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.