改进的冒泡排序

/**

*改进的冒泡排序:按照升序的方式对数组进行排序,此处我们通过添加标记位,对数组中已经有序的序列不再排序,加快了排序的速度

*@ author iamwiam

*

*/

public class Bubble {
    public static void BubbleSort(int[] a){
        int i,j,temp,flag;
        int n = a.length;
        flag = 1;//排序提前结束标志,1表示排序没有结束
        for(i=1; i< n&&flag==1; i++){
            for(j=0; j< n-i ; j++){
                flag = 1;//若以后flag未改变,说明没有发生数组元素交换,说明可提前推出循环
                if(a[j]>a[j+1]){
                    flag = 0;
                    temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                }
            }
        }
    }
     public static void main(String[] args){
        int[] a = {34,25,17,33,5,8,19,66};
        BubbleSort(a);
        for(int i =0; i< a.length;i++){
            System.out.print(a[i]+" ");
        }
    }

    /*public static void BubbleSort(int[] a){
        int nElem = a.length;
        int i,j;
        for(i=1;i<nElem;i++){
            for(j=0;j<nElem-i;j++){
                if(a[j]>a[j+1])
                    swap(a[j],a[j+1]);
            }
        }
    }

    public static void swap(int one, int two){
        int temp;
        temp = one;
        one = two;
        two =temp;
    }*/
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值