arraytest-insertionsort.html

来自「经典的数据结构源代码(java 实现)」· HTML 代码 · 共 24 行

HTML
24
字号
<html><head><title>Code Fragment</title></head><body text=#000000><center></center><br><br><dl><dd><pre>  <font color = #ff0080>/** Insertion sort of an array of characters into non-decreasing order */</font>  <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#8000a0>static</font> <font color=#8000a0><font color=#8000a0>void</font> </font><font color=#0000ff>insertionSort</font>(<font color=#8000a0>char</font>[] a) {     <font color=#8000a0><font color=#8000a0>int</font> </font>n = a.length;    <font color=#ff8000>for</font><font color=#0000ff> </font>(<font color=#8000a0>int</font> i = 1; i &lt; n; i++) {	<font color=#ff0080>// index from the second character in a</font>      <font color=#8000a0><font color=#8000a0>char</font> </font>cur = a[i];			<font color=#ff0080>// the current character to be inserted</font>      <font color=#8000a0><font color=#8000a0>int</font> </font>j = i - 1;			<font color=#ff0080>// start comparing with cell left of i</font>      <font color=#ff8000>while</font><font color=#0000ff> </font>(<font color=#0000ff></font>(j &gt;= 0) &&<font color=#0000ff> </font>(a[j] &gt; cur))	<font color=#ff0080>// while a[j] is out of order with cur</font>	a[j + 1] = a[j--];		<font color=#ff0080>// move a[j] right and decrement j</font>      a[j + 1]=cur;			<font color=#ff0080>// this is the proper place for cur</font>    }  }</dl></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?