sort之冒泡排序(java基礎)

 package sort;
/**
 * 冒泡排序
 * @author Administrator
 *
 */
public class BubbleSort {
 
 
 public void bubbleSort(int a[]){
  int temp;
  //小數上冒
  for(int i=0; i<a.length-1; i++){
   for(int j=i+1; j<a.length; j++){
    if(a[i] > a[j]){
     temp = a[i];
     a[i] = a[j];
     a[j] = temp;
    }
   }
  }
  //大數下沉
//  for(int i=a.length-1; i>0; i--){
//   for(int j=0; j<i; j++){
//    if(a[j] > a[j+1]){
//     temp = a[j+1];
//     a[j+1] = a[j];
//     a[j] = temp;
//    }
//   }
//  }
  
 }
 
 public void printSort(int a[]){
  for(int i=0; i<a.length; i++){
   System.out.print(a[i]+" ");
  }
  
 }
 public static void main(String[] args){
  int[] b = {8,2,5,7,4,53,10,6,23,12,9};
  BubbleSort demo = new BubbleSort();
  demo.bubbleSort(b);
  demo.printSort(b);
 }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章