面试无数,逢试必备——冒泡排序
public class TestBubbleUp { /* 冒泡排序算法 */
public static int[] sort(int[] m) {
int theLenth = m.length; /* 执行theLenth次 */
for (int i = 0; i < theLenth; i++) { /* 每执行一次,将最小的数排在后面 */
for (int j = 0; j < theLenth - i - 1; j++) {
int a = m[j];
int b = m[j + 1];
if (a < b) {
m[j] = b;
m[j + 1] = a;
}
}
}
return m;
}
public static void main(String args[]) {
int[] m = { 9, 1, 0, 13, 100, 39, 88, 32 };
int[] n = sort(m);
for (int i = 0; i < m.length; i++) {
System.out.println(n[i]);
}
}
}
欢迎转载,转载请注明来源和作者,谢谢!