PHP Associative array sorting (Quick sort) _php Tutorial

Source: Internet
Author: User
Use environment and Conditions
There is a case where the associative array inside PHP, if the following array data:
[PHP]
$array = Array (
Array (
' Name ' = ' Xiao ',
' Age ' = 3
),
Array (
' Name ' = ' Wang ',
' Age ' = 1
),
Array (
' Name ' = ' Chen ',
' Age ' = 2
)
);

We want to sort the array for the Age field, and PHP's own functions, no matter what sort, obviously don't meet our needs, so we can write a quick sort code ourselves, and quickly implement our requirements.

Note the situation
There is no pointer in PHP, so when we want to refer to the pass, we can not just like C code, so write quicksort (int *a, int begin, int end), but to use the PHP & operator, the address of the array is passed with the fast sorting function, This makes it possible to implement reference passing in PHP instead of value passing.

Quick Sort Code
[PHP]
Quicksortprocess ($array, 0, Count ($array)-1);
Print_r ($array);

/**
* Description: Get the location of the central point in the quick sort
*/
Function Quickpartition (& $array, $left, $right) {
1. Benchmark definition
$stand = $array [$left];

2. Scan from both ends to the middle until $left = = $right
while ($left < $right) {
while ($left < $right && $array [$right] ["Age"] >= $stand [' age ']) {
$right--;
}
if ($left < $right) {
$array [$left + +] = $array [$right];
}

while ($left < $right && $array [$left] ["Age"] <= $stand [' age ']) {
$left + +;
}
if ($left < $right) {
$array [$right--] = $array [$left];
}
}

3. Get the Hub point location
$array [$left] = $stand;
return $left;
}

/**
* Description: Quick Sort main process function
*/
Function quicksortprocess (& $array, $begin, $end) {
1. Variable definition
$pivot = NULL; Central Point

if ($begin < $end) {
$pivot = Quickpartition ($array, $begin, $end);
Quicksortprocess ($array, $begin, $pivot-1);
Quicksortprocess ($array, $pivot + 1, $end);
}
}

I used this quick sort on the project, very happy, not in vain this October 1 vacation spent n days ac quick sort C code

http://www.bkjia.com/PHPjc/477946.html www.bkjia.com true http://www.bkjia.com/PHPjc/477946.html techarticle use environment and conditions there is a case where the associative array inside PHP, if the following array data: [php] $array = Array (Array (name = Xiao, age = 3), array (name = ...)

  • Related Article

    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.