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