算法回马枪 排序

冒泡排序

/*
* 冒泡排序
*
* 每一趟出来一个最大的数(冒出一个泡泡)
*
* */
public class BubbleSort {

    public static void bubbleSort(int []arr){
        //首先对输入做判断
        if(arr==null||arr.length<2){
            return;
        }

        for(int i=0;i<arr.length-1;i++){      //一共n-1躺排序(n个数需要n-1躺排序)  每一趟冒出来一个最大的数
            for(int j=0;j<arr.length-1-i;j++){    
//每过一趟排序,j的遍历范围缩减1  (最右边的坑已经被最大的数给占了,现在第二大的数准备入从右往左
//数的第二个坑...)
                if(arr[j]>arr[j+1]){
                    MySwap(arr,j,j+1);
                }
            }
        }

    }

    public static void MySwap(int []arr ,int i,int j){
        int temp=arr[i];
        arr[i]=arr[j];
        arr[j]=temp;
    }


    public static void main(String []args){
        int []arr={2,0,4,8,9,1,2,7};

        bubbleSort(arr);

        for (int i = 0; i <arr.length ; i++) {
            System.out.println(arr[i]);
        }
    }
}

时间复杂度: O(n^2)

空间复杂度: O(1)

 

 

选择排序

/*
* 选择排序    每次都选择一个最小的
* */
public class SelectSort {

    public static void selectSort(int []arr){
        for(int i=0;i<arr.length;i++){                  //i来控制遍历的范围   在该范围下选择一个最小的数
            int minIndex=i;
            for (int j = i+1; j < arr.length; j++) {    //找出一个最小的数放在minInde的位置上
                if(arr[j]<arr[minIndex]){
                    MySwap(arr,minIndex,j);    //这样写是有问题的
                }
            }
        }
    }

    //优化版   遍历完所有数,确定minindex  在Swap
    public static void selectSort_edition2(int []arr){
        for(int i=0;i<arr.length;i++){                  //i来控制遍历的范围   在该范围下选择一个最小的数
            int minIndex=i;
            for (int j = i+1; j < arr.length; j++) {    //找出一个最小的数放在minIndex的位置上
                if(arr[j]<arr[minIndex]){
                    minIndex=j;
                }
            }
            MySwap(arr,i,minIndex);   //确定一个最小的只交换一次
        }
    }


    public static void MySwap(int []arr ,int i,int j){
        int temp=arr[i];
        arr[i]=arr[j];
        arr[j]=temp;
    }


    public static void main(String []args){
        int []arr={2,0,42,8,9,11,2,7};

        selectSort_edition2(arr);

        for (int i = 0; i <arr.length ; i++) {
            System.out.println(arr[i]);
        }
    }
}

时间复杂度: O(n^2)

 

插入排序

斗地主

类似你手里的牌是已经排好序的,你新起一张牌,这张牌与你手里的最大的一张牌比较,如果大,就放你右边,如果小,就和手里最大的牌之前的一张牌比较...

根据当前的大小能比较到哪个位置,插入

 

/*
插入排序
* */
public class InsertSort {

    public static void insertSort(int []arr){

        for (int i = 1; i < arr.length; i++) {   //考虑的当前数i   往前面有序区插入(0  到 i-1)
            for (int j = i-1; j >=0 ; j--) {      //位置i的前一个数
                if(arr[j]>arr[j+1]){
                    MySwap(arr,j,j+1);
                }
            }
        }
    }

    //添加标记
    public static void insertSort_edition2(int []arr){
        boolean isSwaped  =false;  //i和i-1是否交换了    如果没有交换直接break
        for (int i = 1; i < arr.length; i++) {   //考虑的当前数i   往前面有序区插入(0  到 i-1)
            for (int j = i-1; j >=0 ; j--) {      //位置i的前一个数
                if(arr[j]>arr[j+1]){
                    MySwap(arr,j,j+1);
                    isSwaped=true;
                }
                if(!isSwaped){
                    break;
                }
            }
        }
    }

    public static void MySwap(int []arr ,int i,int j){
        int temp=arr[i];
        arr[i]=arr[j];
        arr[j]=temp;
    }


    public static void main(String []args){
        int []arr={2,0,42,8,9,11,2,7};

        insertSort_edition2(arr);

        for (int i = 0; i <arr.length ; i++) {
            System.out.println(arr[i]);
        }
    }
}

注意:选择排序和冒泡排序跟数据状况是没有关系的 严格的 O(n^2)

 

插入排序的好坏要根据数据状况决定的

如果已经排好序了,插入排序O(n)

如果是逆序,插入排序O(n^2)

 

于是有了最好情况,最差情况

这样的算法按照最差的情况估计算法复杂度,因此插入排序就是O(n^2)

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值