给我发信息!

鲍博博客园

程序员学习成长记录史

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

/**
* 冒泡排序算法:包含从小到大 和 从大到小
* @author Administrator
*
*/

public class MaoPaoTest {

public static void main(String[] args) {
   oneMethod();
   System.out.println();
   System.out.println();
   System.out.println();
   threeMethod();
}

/**
* 冒泡排序从小到大
*
*/
public static void oneMethod() {
   int array[] = {-5,-9,2,5,10,7,895};
  
   for(int i = 0 ; i < array.length-1 ; i ++) {
    if(array[i] > array[i+1]) {
     int temp = array[i];
     array[i] = array[i+1];
     array[i+1] = temp;
    }
   }
   for(int i = 0 ; i < array.length ; i ++) {
    System.out.print(” “+array[i]+” “);
   }
}

/**
* 冒泡排序从大到小
*
*/
public static void threeMethod() {
   int array[] = {-5,-9,2,5,10,7,895};
  
   for(int i = 0 ; i < array.length ; i ++) {
    for(int j = i +1 ; j < array.length ; j ++) {
     if(array[i] < array[j]) {
      int temp = array[i];
      array[i] = array[j];
      array[j] = temp;
     }
    }
   }
   for(int i = 0 ; i < array.length ; i ++) {
    System.out.print(” “+array[i]+” “);
   }
}

}

posted on 2011-01-03 22:04  鲍博博客园  阅读(309)  评论(0)    收藏  举报