黑马程序员技术交流社区

标题: 刚学会的冒泡 [打印本页]

作者: lshaizj    时间: 2015-5-4 21:36
标题: 刚学会的冒泡
直接上代码
  1. class Max
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 int [] arr = {18,27,6,81,101,15,2};//7
  6.                 System.out.print("冒泡前:");
  7.                 for (int a=0;a<arr.length ;a++ )
  8.                 {
  9.                         System.out.print(" "+arr[a]);
  10.                 }
  11.                 System.out.println();
  12.                 getMax(arr);
  13.         }
  14.         public static void getMax(int [] a)
  15.         {
  16.                 int temp=0;
  17.                 for (int b=0;b<a.length ;b++ )
  18.                 {
  19.                         for (int c=b+1;c<a.length;c++ )
  20.                         {
  21.                                 if (a[b]>a[c])
  22.                                 {
  23.                                         temp=a[b];
  24.                                         a[b]=a[c];
  25.                                         a[c]=temp;
  26.                                 }
  27.                         }
  28.                 }
  29.                 System.out.print("冒泡后:");
  30.                 for (int b=0;b<a.length;b++ )
  31.                 {
  32.                         System.out.print(" "+a[b]);
  33.                 }
  34.         }
  35. }
复制代码







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