黑马程序员技术交流社区
标题:
冒泡排序法
[打印本页]
作者:
唐溪永
时间:
2012-2-21 09:34
标题:
冒泡排序法
//相邻两个数比较,将最小或最大的放到后面,最后面数的不参与比较
public class BubbleSort {
private static int al[] = new int[10];
public BubbleSort() {
al[0]=2;
al[1]=3;
al[2]=23;
al[3]=45;
al[4]=1;
al[5]=67;
al[6]=23;
al[7]=80;
al[8]=35;
al[9]=72;
}
public static void main(String[] args) {
BubbleSort bs = new BubbleSort();
System.out.println("排序前:");
display(al);
for(int i=0;i<al.length;i++) {
for (int j = 0; j < al.length-i-1; j++) {
if(al[j]>al[j+1]) {
swap(j,j+1);
}
}
}
System.out.println();
System.out.println("排序后:");
display(al);
}
private static void display(int[] al2) {
for (int i = 0; i < al2.length; i++) {
System.out.print(al2[i]+" ");
}
}
private static void swap(int i, int j) {
int temp = al[i];
al[i]= al[j];
al[j] = temp;
}
}
复制代码
作者:
温昌寿
时间:
2012-2-21 09:50
冒泡的效率不高
作者:
花开~的季节
时间:
2012-2-21 17:52
用快速排序
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2