黑马程序员技术交流社区

标题: 冒泡排序法 [打印本页]

作者: 唐溪永    时间: 2012-2-21 09:34
标题: 冒泡排序法
  1. //相邻两个数比较,将最小或最大的放到后面,最后面数的不参与比较
  2. public class BubbleSort {
  3.         private static int al[] = new int[10];
  4.         public BubbleSort() {
  5.                 al[0]=2;
  6.                 al[1]=3;
  7.                 al[2]=23;
  8.                 al[3]=45;
  9.                 al[4]=1;
  10.                 al[5]=67;
  11.                 al[6]=23;
  12.                 al[7]=80;
  13.                 al[8]=35;
  14.                 al[9]=72;
  15.         }
  16.         public static void main(String[] args) {
  17.                 BubbleSort bs = new BubbleSort();
  18.                 System.out.println("排序前:");
  19.                 display(al);
  20.                
  21.                 for(int i=0;i<al.length;i++) {
  22.                
  23.                         for (int j = 0; j < al.length-i-1; j++) {
  24.                                
  25.                                 if(al[j]>al[j+1]) {
  26.                                         swap(j,j+1);
  27.                                 }
  28.                         }
  29.                 }
  30.                 System.out.println();
  31.                 System.out.println("排序后:");
  32.                 display(al);
  33.         }
  34.         private static void display(int[] al2) {
  35.                 for (int i = 0; i < al2.length; i++) {
  36.                         System.out.print(al2[i]+"  ");
  37.                 }
  38.         }
  39.         private static void swap(int i, int j) {
  40.                 int temp = al[i];
  41.                 al[i]= al[j];
  42.                 al[j] = temp;
  43.         }
  44. }
复制代码

作者: 温昌寿    时间: 2012-2-21 09:50
冒泡的效率不高
作者: 花开~的季节    时间: 2012-2-21 17:52
用快速排序




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2