PHP Two algorithm code _php tutorial without built-in functions for array sorting

Source: Internet
Author: User
A friend to find a job to meet the questions, notes.
It is very likely that I will meet in the future.
Problem: PHP does not use built-in functions for array sorting, possibly descending or ascending
The first method: the legendary bubbling method
Copy CodeThe code is as follows:
function ArraySort ($data, $order = ' asc ') {
ASC Ascending desc Descending
$temp = Array ();
$count = count ($data);
if ($count <= 0)
return false; The incoming data is incorrect
if ($order = = ' asc ') {
for ($i = 0; $i < $count; $i + +) {
for ($j = $count-1; $j > $i; $j-) {
if ($data [$j] < $data [$j-1]) {
Swap the location of two data
$temp = $data [$j];
$data [$j] = $data [$j-1];
$data [$j-1] = $temp;
}
}
}
} else {
for ($i = 0; $i < $count; $i + +) {
for ($j = $count-1; $j > $i; $j-) {
if ($data [$j] > $data [$j-1]) {
$temp = $data [$j];
$data [$j] = $data [$j-1];
$data [$j-1] = $temp;
}
}
}
}
return $data;
}
$data = Array (7, 5, 3, 8, 9, 1, 5, 3, 1, 24, 3, 87, 0, 33, 1, 12, 34, 54, 66, 32);
Var_dump (ArraySort ($data)); Ascending
Echo ('
');
Var_dump (ArraySort ($data, ' desc '));//Descending

The second method: Do not know what name to take a good, called the insertion Method! 囧
Copy CodeThe code is as follows:
function Arraysort3 ($data, $order = ' asc ') {
Currently only in ascending order.
$count = count ($data);
for ($i = 1; $i < $count; $i + +) {
$temp = $data [$i];
$j = $i-1;
while ($data [$j] > $temp) {
$data [$j + 1] = $data [$j];
$data [$j] = $temp;
$j--;//Why to Decrement: Judging from the high position
}
}
return $data;
}
$data = Array (7, 5, 3, 8, 9, 1, 5, 3, 1, 24, 3, 87, 0, 33, 1, 12, 34, 54, 66, 32);
Var_dump (Arraysort3 ($data)); Ascending

http://www.bkjia.com/PHPjc/321147.html www.bkjia.com true http://www.bkjia.com/PHPjc/321147.html techarticle a friend to find a job to meet the questions, notes. It is very likely that I will meet in the future. Problem: PHP does not use built-in functions to sort the array, may be descending or ascending the first method ...

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