常用排序算法

前言:在了解各种排序算法之前,首先需要了解什么是时间复杂度、什么是空间复杂度、什么是常数时间操作。

表达式:

 

总结: (n+1)*(n/2)*2 +n

以下三种排序都是O(n^2),但是最后一个插入排序性能更好,可能会为O(n):

1.选择排序

 

方法1:

package lqz;

/**
 * Created by liu on 2021/7/20.
 */
public class SelectSort {
    public static void main(String[] args) {
        Integer[] integers = {1, 5, 8, 9, 2, 3, 4, 6};
        getIntegerSort(integers);
        for (int m =0 ;m< integers.length;m++){
            System.out.println(integers[m]);
        }
    }
    public static void  getIntegerSort(Integer[] integers){
        if(integers == null || integers.length<2){
            return;
        }
        Integer num;
        //0  ---  N-1
        //1  --- N-1
        //.....
        //N-2  --- N-1
        for (int i = 0;i<integers.length-1;i++){
            for (int j = i+1;j<integers.length;j++){
//                integers[i] = integers[i] < integers[j] ? integers[i]: integers[j];
                if(integers[i]> integers[j]){
                    num = integers[i];
                    integers[i] = integers[j];
                    integers[j] = num;
                }
            }
        }
    }
}

方法2:

package lqz;

/**
 * Created by liu on 2021/7/20.
 */
public class SelectSort2 {
    public static void main(String[] args) {
        Integer[] integers = {1, 5, 8, 9, 2, 3, 4, 6};
        getIntegerSort(integers);
        for (int m =0 ;m< integers.length;m++){
            System.out.println(integers[m]);
        }
    }
    public static void  getIntegerSort(Integer[] integers){
        if(integers == null || integers.length<2){
            return;
        }
        Integer minNumIndex;
        //0  ---  N-1
        //1  --- N-1
        //.....
        //N-2  --- N-1
        for (int i = 0;i<integers.length-1;i++){
            minNumIndex = i;
            for (int j = i+1;j<integers.length;j++){
                minNumIndex = integers[minNumIndex] <=  integers[j] ? minNumIndex: j;
            }
            swapSort(integers,i,minNumIndex);
        }
    }
    public static  void  swapSort(Integer[] integers,int i,int minNumIndex){
        if(i != minNumIndex){
        Integer middle = integers[i];
        integers[i] = integers[minNumIndex];
        integers[minNumIndex] = middle;
        }
    }
}

2.冒泡排序

package lqz;

/**
 * Created by liu on 2021/7/20.
 */
public class BubbleSort {
    public static void main(String[] args) {
        Integer[] integers = {1, 5, 8, 9, 2, 3, 4, 6};
        //0 --- N-1
        //0 --- N-2
        //...
        //0 --- 1
        for (int i = integers.length-1 ;i>0 ;i--){
//            Integer maxMumIndex = i;
            for (int m = 0;m < i;m++){
                if(integers[m] > integers[m+1]){
                    int middle = integers[m];
                    integers[m] = integers[m+1];
                    integers[m+1] = middle;
                }
            }
        }
        for (int n=0 ;n< integers.length;n++){
            System.out.println(integers[n]);
        }
    }
}

3.插入排序

就是打牌的例子:

方法一:

package lqz;

/**
 * Created by liu on 2021/7/20.
 */
public class InsertSort {
    public static void main(String[] args) {
        Integer[] integers = {1, 5, 8, 9, 2, 3, 4, 6};
        //0
        //0- 1
        //0- 2
        //..
        //0- N-1
        for (int a= 1;a<integers.length;a++){
            for (int m = a-1;m<a;m--){
                if(integers[m] >  integers[m+1]){
                    int middle = integers[m];
                    integers[m] = integers[m+1];
                    integers[m+1] = middle;
                }else {
                    break;
                }
            }
        }
        for (int n=0 ;n< integers.length;n++){
            System.out.println(integers[n]);
        }
    }
}

 

方法二:

package lqz;

/**
 * Created by liu on 2021/7/20.
 */
public class InsertSort2 {
    public static void main(String[] args) {
        Integer[] integers = {1, 5, 8, 9, 2, 3, 4, 6};
        //0- 1
        //0- 2
        //..
        //0- N-1
        for (int a= 1;a<integers.length;a++){
            for (int m = a-1;integers[m] > integers[m+1] && m>=0;m--){
                    int middle = integers[m];
                    integers[m] = integers[m + 1];
                    integers[m + 1] = middle;
            }
        }
        for (int n=0 ;n< integers.length;n++){
            System.out.println(integers[n]);
        }
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值