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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Mac version
God-level code editing software (SublimeText3)
