Home >Backend Development >PHP Tutorial >Java, java official website_PHP tutorial
<span>int</span> [] array1 = {1,3,5,7,9,10,2,15,154,10,2,188,200};<span>//</span><span>定义一个数组,内容为混乱大小</span> <span>int</span> index = 0;<span>//</span><span>定义一个最大值或最小值的位置</span> <span>int</span> keyValue = 0;<span>//</span><span>定义最大值或最小值</span> <span>int</span> temp = 0;<span>//</span><span>定义一个临时存储变量</span> <span>for</span>(<span>int</span> i=0;i<array1.length;i++){<span>//</span><span>依次循环所有数据</span> index = i;<span>//</span><span>将i位置赋值给index</span> keyValue = array1[index];<span>//</span><span>将i位置的值赋值给keyValue</span> <span>for</span>(<span>int</span> y=i;y<array1.length;y++){<span>//</span><span>再次循环所有变量,不过这次是长度起始长度为i,最大长度不变</span> <span>if</span>(array1[y] < keyValue){<span>//</span><span>如果循环到的值小于i位置的值</span> index = y;<span>//</span><span>位置重新定义为当前位置</span> keyValue = array1[y];<span>//</span><span>最小值为当前位置</span> <span> } } temp </span>= array1[i];<span>//</span><span>将i位置的值赋值给临时变量(最大值)</span> array1[i] = array1[index];<span>//</span><span>将i位置重新定义为index(最小)位置的</span> array1[index] = temp;<span>//</span><span>将最大值重新定义给index位置,实现交换过程</span> <span> } </span><span>for</span>(<span>int</span> p=0;p<array1.length;p++<span>){ System.out.print(array1[p]</span>+" "<span>); }</span>
<?<span>php </span><span>/*</span><span> 选择排序::对数组进行从小到大的排序 </span><span>*/</span> <span>$array</span> = <span>array</span>(2, 100, 3, 50, 48, 99, 502, 30, 27, 15, 16, 300, 11, 9, 5, 4, 42<span>); </span><span>for</span> (<span>$i</span> = 0; <span>$i</span> < <span>count</span>(<span>$array</span>); <span>$i</span>++<span>) { </span><span>$index</span> = <span>$i</span>; <span>//</span><span>将index设置为当前循环步长</span> <span>$keyValue</span> = <span>$array</span>[<span>$index</span>]; <span>//</span><span>将最小值设置为当前</span> <span>for</span> (<span>$y</span> = <span>$i</span>; <span>$y</span> < <span>count</span>(<span>$array</span>); <span>$y</span>++<span>) { </span><span>if</span>(<span>$array</span>[<span>$y</span>] < <span>$keyValue</span>){ <span>//</span><span>如果循环余下的数据,有比i更小的值</span> <span>$index</span> = <span>$y</span>; <span>//</span><span>记录最小值的索引位置</span> <span>$keyValue</span> = <span>$array</span>[<span>$index</span>];<span>//</span><span>将最小值设置为最小值索引位置的值</span> <span> } } </span><span>$temp</span> = <span>$array</span>[<span>$i</span>]; <span>//</span><span>将当前循环的值赋值给临时变量(最大值)</span> <span>$array</span>[<span>$i</span>] = <span>$array</span>[<span>$index</span>];<span>//</span><span>将最小值赋值给当前位置</span> <span>$array</span>[<span>$index</span>] = <span>$temp</span>;<span>//</span><span>将最小值的位置填充上最大值</span> <span>} </span><span>print_r</span>(<span>$array</span><span>); </span>?>