Rumah > php教程 > php手册 > teks badan

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

WBOY
Lepaskan: 2016-05-20 11:39:17
asal
1218 orang telah melayarinya

定义数组长度变量$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>);
Salin selepas log masuk

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>
Salin selepas log masuk

 

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan