Home  >  Article  >  php教程  >  [PHP] 看博客学习插入排序 - 陶士涵

[PHP] 看博客学习插入排序 - 陶士涵

WBOY
WBOYOriginal
2016-05-20 11:39:171219browse

定义数组长度变量$len,使用count()函数,参数:数组

for循环数组,条件:从第二个开始,遍历数组,循环内

         定义临时变量$temp,赋值当前元素

         for循环数组,条件:遍历当前元素前面的所有元素

         判断当前元素与它前面的元素的大小,利用临时变量,转换变量

PHP版:

<span style="color: #000000;">php
</span><span style="color: #800080;">$arr</span>=<span style="color: #0000ff;">array</span>(2,3,4,1,5<span style="color: #000000;">);
</span><span style="color: #0000ff;">function</span> insert_sort(<span style="color: #800080;">$arr</span><span style="color: #000000;">){
    </span><span style="color: #800080;">$len</span>=<span style="color: #008080;">count</span>(<span style="color: #800080;">$arr</span><span style="color: #000000;">);
    </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=1;<span style="color: #800080;">$i</span>$len;<span style="color: #800080;">$i</span>++<span style="color: #000000;">){
        </span><span style="color: #800080;">$temp</span>=<span style="color: #800080;">$arr</span>[<span style="color: #800080;">$i</span><span style="color: #000000;">];
        </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$j</span>=<span style="color: #800080;">$i</span>-1;<span style="color: #800080;">$j</span>>=0;<span style="color: #800080;">$j</span>--<span style="color: #000000;">){
            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$temp</span>$arr[<span style="color: #800080;">$j</span><span style="color: #000000;">]){
                </span><span style="color: #800080;">$arr</span>[<span style="color: #800080;">$j</span>+1]=<span style="color: #800080;">$arr</span>[<span style="color: #800080;">$j</span><span style="color: #000000;">];
                </span><span style="color: #800080;">$arr</span>[<span style="color: #800080;">$j</span>]=<span style="color: #800080;">$temp</span><span style="color: #000000;">;
            }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
            }
        }
    }
    </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$arr</span><span style="color: #000000;">;
}
</span><span style="color: #800080;">$arr</span>=insert_sort(<span style="color: #800080;">$arr</span><span style="color: #000000;">);
</span><span style="color: #008080;">print_r</span>(<span style="color: #800080;">$arr</span>);

java版:

<span style="color: #0000ff;">package</span><span style="color: #000000;"> com.tsh.mthread;

</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Home {

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

 

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