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

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

WBOY
WBOY원래의
2016-05-20 11:39:171216검색

定义数组长度变量$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>

 

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.