冒泡排序法(java版)

public class BubbleSort {
 /**
  * 冒泡排序法
  */
 public static void bubbleSort(int a[]){
    int temp;
    for(int i=0;i<a.length-1;i++){
     for(int j=0;j<a.length-i-1;j++){
      if(a[j]>a[j+1]){
      temp=a[j+1];
      a[j+1]=a[j];
      a[j]=temp;}
     }
    }
   }

 public static void main(String[] args) {
  int a[] = { 2, 1, 4, 7, 5 };
  // 调用冒泡排序方法
  bubbleSort(a);
  // 遍历排序后的数组a
  for (int i = 0; i < a.length; i++) {
   System.out.print(a[i] + " ");
  }
 }
}

posted @ 2013-03-12 09:28  zhgs_cq  阅读(120)  评论(0编辑  收藏  举报