Home >Backend Development >PHP Tutorial >PHP insertion sort implementation code_PHP tutorial

PHP insertion sort implementation code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:12:33650browse

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

Copy code The code is as follows:

$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

TechArticleAlgorithm description: ⒈ Starting from the first element, the element can be considered to have been sorted ⒉ Take out the next element, Scan from back to front in the sorted sequence of elements ⒊ If the element...

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn