Home  >  Article  >  Backend Development  >  [PHP] Read the blog to learn insertion sort

[PHP] Read the blog to learn insertion sort

WBOY
WBOYOriginal
2016-07-29 08:51:54953browse

Define the array length variable $len, use the count() function, parameter: array

for loop array, condition: starting from the second one, traverse the array, within the loop

                                      Define the temporary variable $temp, assign the current element

       Loop array, condition: traverse all elements before the current element

Determine the size of the current element and the elements before it, use temporary variables, convert variables

[PHP] 看博客学习插入排序

PHP version:

<?<span>php
</span><span>$arr</span>=<span>array</span>(2,3,4,1,5<span>);
</span><span>function</span> insert_sort(<span>$arr</span><span>){
    </span><span>$len</span>=<span>count</span>(<span>$arr</span><span>);
    </span><span>for</span>(<span>$i</span>=1;<span>$i</span><<span>$len</span>;<span>$i</span>++<span>){
        </span><span>$temp</span>=<span>$arr</span>[<span>$i</span><span>];
        </span><span>for</span>(<span>$j</span>=<span>$i</span>-1;<span>$j</span>>=0;<span>$j</span>--<span>){
            </span><span>if</span>(<span>$temp</span><<span>$arr</span>[<span>$j</span><span>]){
                </span><span>$arr</span>[<span>$j</span>+1]=<span>$arr</span>[<span>$j</span><span>];
                </span><span>$arr</span>[<span>$j</span>]=<span>$temp</span><span>;
            }</span><span>else</span><span>{
                </span><span>break</span><span>;
            }
        }
    }
    </span><span>return</span><span>$arr</span><span>;
}
</span><span>$arr</span>=insert_sort(<span>$arr</span><span>);
</span><span>print_r</span>(<span>$arr</span>);

java version:

<span>package</span><span> com.tsh.mthread;

</span><span>public</span><span>class</span><span> Home {

    </span><span>public</span><span>static</span><span>void</span><span> main(String[] args) {
        </span><span>int</span>[] arr=<span>new</span><span>int</span>[]{2,3,4,1,5<span>};
        arr</span>=<span>insert_sort(arr);
        </span><span>for</span>(<span>int</span><span> x:arr){
            System.out.println(x);
        }
        
    }
    </span><span>/**</span><span>     * 插入排序
     * </span><span>@param</span><span> arr
     * </span><span>@return</span><span>*/</span><span>public</span><span>static</span><span>int</span>[] insert_sort(<span>int</span><span>[] arr) {
        </span><span>int</span> len=<span>arr.length;
        </span><span>for</span>(<span>int</span> i=1;i<len;i++<span>){
            </span><span>int</span> temp=<span>arr[i];
            </span><span>for</span>(<span>int</span> j=i-1;j>=0;j--<span>){
                </span><span>if</span>(temp<<span>arr[j]){
                    arr[j</span>+1]=<span>arr[j];
                    arr[j]</span>=<span>temp;
                }</span><span>else</span><span>{
                    </span><span>break</span><span>;
                }
            }
        }
        </span><span>return</span><span> arr;
    }
}</span>

The above introduces [PHP] Read the blog to learn insertion sort, including insertion sort and PHP content. I hope it will be helpful to friends who are interested in PHP tutorials.

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