PHP array sorting

Source: Internet
Author: User

"Internal sorting Method"

First, Exchange sorting method

1. Bubble Sorting method

<?PHP//Bubble Sort method (small to large)    $arr=Array(55,20,10,8,2,0); functionBubblesort (&$arr) {        //after the second number is sorted, the first number does not need to repeat the loop again, so the number of cycles < count ($arr)-1         for($i= 0;$i<Count($arr)-1;$i++) {            //each element is compared in turn, with two elements comparing size, that is, the number of cycles < count ($arr)-1, followed by a sorted number without the next cyclic sort, that is, COUNT ($arr)-$i-1             for($j= 0;$j<Count($arr)-$i-1;$j++) {                if($arr[$j]>$arr[$j+1]) {                    $temp=$arr[$j]; $arr[$j] =$arr[$j+1]; $arr[$j+1] =$temp; }//Echo ($arr [$j]). " ";            }//Print_r ($arr);//echo ' <br/> ';}} bubblesort ($arr); Print_r($arr);?>

2. Quick Sort Method

Ii. Choice of sorting method

Three, inserting type sorting method

1. Insert Sort method

2. Hill Sort method

3. Two binary tree sorting method

"External sorting: Sort by external file"

Note: If the sorting method is encapsulated into a function so that it can be called directly at a later time, use the return value, or the shape to participate in the address character "&", such as:& $arr

PHP array sorting

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.