"Algorithmic" PHP implements bubbling sorting and quick sorting--anti-forgetting

Source: Internet
Author: User

There is no such feeling, although the sorting algorithm is simple, but did not see once, and then forget, so it is necessary to use the actual code to run the implementation, just remember firmly, for this mark

Requirements: Arrange the elements in the array, from large to small
$a = Array (11, 22, 44, 66, 99, 88);

1. Bubble sort
/* Compares the 1th number to the 2nd number, and, if it is less than the 2nd number, exchanges the position, in turn, with these numbers.
* followed by a 2nd number to do the same thing until the last number
*/

Requirements: Arrange the elements in the array, from large to small
$a = Array (11, 22, 44, 66, 99, 88);
$sortArray = Bubblesort ($a);
Print_r ($sortArray);


functionBubblesort ($array) {/*{{{*/ $oldArray=$array; $newArray=Array(); $count=Count($oldArray); for($i= 0;$i<$count;$i++) {/*{{{*/ //Initialize $newArray[$i] =$oldArray[$i]; for($j=$i+1;$j<$count;$j++) { if($oldArray[$j] >$newArray[$i]) { $newArray[$i] =$oldArray[$j]; //Swap Location $tmp=$oldArray[$i]; $oldArray[$i] =$oldArray[$j]; $oldArray[$j] =$tmp; } } }/*}}}*/ return $newArray; }/*}}}*/

2. Quick Sort
/* Use the first number as the benchmark, and the number to the left of the array, which is larger than the number to the right of the array, and then do the recursive operation.
* Finally, the left array, this number and the right array to do an array of array_merge operation on the line
*/

Requirements: Arrange the elements in the array, from large to small
$a = Array (11, 22, 44, 66, 99, 88);
$sortArray = Bubblesort ($a);
Print_r ($sortArray);

functionFastsort ($oldArray) {/*{{{*/ $newArray=Array(); $count=Count($oldArray); if($count<= 1)return $oldArray; $stand=$oldArray[0]; //Initialize $left _array=Array(); $right _array=Array(); for($i= 1;$i<$count;$i++) {/*{{{*/ if($oldArray[$i] >$stand) { $left _array[] =$oldArray[$i]; } Else { $right _array[] =$oldArray[$i]; } }/*}}}*/ //Recursive invocation $left _array= Fastsort ($left _array); $right _array= Fastsort ($right _array); //can also be looped into the new array $newArray=Array_merge($left _array,Array($stand),$right _array); return $newArray; }/*}}}*/


"Algorithmic" PHP implements bubbling sorting and quick sorting--anti-forgetting

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.