- <p>class Array2 {</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>public static void main(String[] args) {</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>int [] arr = {1,3,5,2,6,8,3,9,3,0};</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println(arr.length);</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println("排序前的数组");</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>//for (int x = 0 ; x <= arr.length;x++ ) </p><p><span class="Apple-tab-span" style="white-space:pre"> </span>//打印数组</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>for (int x : arr ) {</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.print(x+" ");</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>} //换行</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println(); </p><p><span class="Apple-tab-span" style="white-space:pre"> </span>for (int x = 0;x < arr.length ;x++ ) {</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>int index = x;</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>int value = arr[x];</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>for (int y = x;y < arr.length ;y++ ) {</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>if (arr[y] < value) {</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>index = y;</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>value = arr[y];</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>}</p><p>
- </p><p><span class="Apple-tab-span" style="white-space:pre"> </span>}</p><p><span class="Apple-tab-span" style="white-space:pre"> </span> int temp = arr[x];</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>arr[x] = arr[index];</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>arr[index] = temp;</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>}</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println("排序后的数组");</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>for (int b :arr ) {</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.print(b+" ");</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>}</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>}</p><p>}</p>
复制代码 int temp = arr[x]; arr[x] = arr[index]; arr[index] = temp; 这段代码 不是很懂 求 指教
|
|