/*
* 冒泡法排列
*/
public class First_bubbles {
public static void main(String[] args) {
int arr[] = { 0, 1, 6, -1, 9, 45, 90, -76, 2, 12, 7, 89, -34 };
bubble bble = new bubble();
bble.sort(arr);
System.out.println("use the bubble number compare and get the answer");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
/*
* 定义一个类,将其方法封装
*/
class bubble {
public void sort(int arr[]) {
int temp = 0;
for (int i = 0; i < arr.length - 1; ++i) {
for (int j = 0; j < arr.length - 1 - i; ++j) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}作者: 周世阳 时间: 2012-8-14 02:22
i++与++i的区别和C语言是一样的:
i++是先使用i的值然后i才自加1
++i是i的值先自加1再使用i的值作者: 王少岩 时间: 2012-8-14 08:44