java for循环排序_java编程问题。用两个for循环来把输入的整数从小到大排序。
展開全部
這樣的代碼看的費勁
數(shù)組排序62616964757a686964616fe59b9ee7ad9431333363366165的方法有冒泡和選擇比較的好理解:
代碼如下:
package com.tx.wl.p10;
public class ArrayDemo {
public static void main(String[] args) {
int[] a = { 3, 43, 22, 4, 22, 31, 23 };
System.out.println("遍歷數(shù)組:");
showArray(a);
System.out.println("冒泡排序:");
maoPao(a);
showArray(a);
System.out.println("選擇排序:");
xuanze(a);
showArray(a);
}
private static void xuanze(int[] a) {
int[] b = a;
for (int i = 0; i < b.length; i++) {
int index = i;
for (int j = i; j < b.length; j++) {
if (a[index] > b[j]) {
index = j;
}
}
int temp = b[index];
b[index] = b[i];
b[i] = temp;
}
}
private static void maoPao(int[] a) {
int[] c = a;
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < c.length - i - 1; j++) {
if (c[j] > c[j + 1]) {
int temp = c[j];
c[j] = c[j + 1];
c[j + 1] = temp;
}
}
}
}
private static void showArray(int[] a) {
for (int aa : a) {
System.out.print(aa + " ");
}
System.out.println();
}
}
理解型記憶,里面的遍歷用的是增強for,原理就相當(dāng)于
for(int i=0; i
System.ou.tprintln(a[i]);
}
總結(jié)
以上是生活随笔為你收集整理的java for循环排序_java编程问题。用两个for循环来把输入的整数从小到大排序。的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 姓陈的孝字辈和姓汪的锡字辈哪个大?
- 下一篇: java session持久化_Sess