Java BubbleSort(冒泡排序)

来源:互联网 发布:淘宝流量卡是真的吗 编辑:程序博客网 时间:2024/04/28 19:28

package lwm.algorithm.sort;

import java.util.Arrays;

public class BubbleSort {
  private boolean flag=true;//循环结束标记
  private int count=0;//一趟排序中比较的次数
  private int temp;
    public void BSort(int[] a){
     count=a.length;
     while (flag){
      flag=false;
      for(int I=1;I<=count-1;I++){
       if(a[I]<a[I-1]){
        temp=a[I];
        a[I]=a[I-1];
        a[I-1]=temp;
        flag=true;
       }
      }
      System.out.println("第"+(a.length+1-count)+"次数"+Arrays.toString(a));
      count--;
     }
    }
}

原创粉丝点击