Home >Backend Development >PHP Tutorial >PHP insertion sort implementation code_PHP tutorial
Algorithm description:
⒈ Starting from the first element, this element can be considered to have been sorted
⒉ Take out the next element and scan from back to front in the sorted element sequence
⒊ If the element (sorted) is greater than the new element, move the element to the next position
⒋ Repeat step 3 until you find the position where the sorted element is less than or equal to the new element
⒌ Insert the new element into the next position
⒍ Repeat step 2
$arr =array(123,0,5,-1,4,15);
function insertSort(&$arr){
Insert the compared number
$insertVal=$arr[$i];
through Find location
while($insertIndex>=0 && $insertVal<$arr[$insertIndex]){
🎜>
;
insertSort($arr);
print_r($arr);
http://www.bkjia.com/PHPjc/326682.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/326682.html